forked from TheServat/Pikaday-Jalali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pikaday.js
1843 lines (1617 loc) · 58 KB
/
pikaday.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
JDate
*/
/*
Converts a Gregorian date to Jalaali.
*/
function toJalaali(gy, gm, gd) {
if (Object.prototype.toString.call(gy) === '[object Date]') {
gd = gy.getDate()
gm = gy.getMonth() + 1
gy = gy.getFullYear()
}
return d2j(g2d(gy, gm, gd))
}
/*
Converts a Jalaali date to Gregorian.
*/
function toGregorian(jy, jm, jd) {
return d2g(j2d(jy, jm, jd))
}
/*
Checks whether a Jalaali date is valid or not.
*/
function isValidJalaaliDate(jy, jm, jd) {
return jy >= -61 && jy <= 3177 &&
jm >= 1 && jm <= 12 &&
jd >= 1 && jd <= jalaaliMonthLength(jy, jm)
}
/*
Is this a leap year or not?
*/
function isLeapJalaaliYear(jy) {
return jalCal(jy).leap === 0
}
/*
Number of days in a given month in a Jalaali year.
*/
function jalaaliMonthLength(jy, jm) {
if (jm <= 6) return 31
if (jm <= 11) return 30
if (isLeapJalaaliYear(jy)) return 30
return 29
}
/*
This function determines if the Jalaali (Persian) year is
leap (366-day long) or is the common year (365 days), and
finds the day in March (Gregorian calendar) of the first
day of the Jalaali year (jy).
@param jy Jalaali calendar year (-61 to 3177)
@return
leap: number of years since the last leap year (0 to 4)
gy: Gregorian year of the beginning of Jalaali year
march: the March day of Farvardin the 1st (1st day of jy)
@see: http://www.astro.uni.torun.pl/~kb/Papers/EMP/PersianC-EMP.htm
@see: http://www.fourmilab.ch/documents/calendar/
*/
function jalCal(jy) {
// Jalaali years starting the 33-year rule.
var breaks = [-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210
, 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178
]
, bl = breaks.length
, gy = jy + 621
, leapJ = -14
, jp = breaks[0]
, jm
, jump
, leap
, leapG
, march
, n
, i
if (jy < jp || jy >= breaks[bl - 1])
throw new Error('Invalid Jalaali year ' + jy)
// Find the limiting years for the Jalaali year jy.
for (i = 1; i < bl; i += 1) {
jm = breaks[i]
jump = jm - jp
if (jy < jm)
break
leapJ = leapJ + div(jump, 33) * 8 + div(mod(jump, 33), 4)
jp = jm
}
n = jy - jp
// Find the number of leap years from AD 621 to the beginning
// of the current Jalaali year in the Persian calendar.
leapJ = leapJ + div(n, 33) * 8 + div(mod(n, 33) + 3, 4)
if (mod(jump, 33) === 4 && jump - n === 4)
leapJ += 1
// And the same in the Gregorian calendar (until the year gy).
leapG = div(gy, 4) - div((div(gy, 100) + 1) * 3, 4) - 150
// Determine the Gregorian date of Farvardin the 1st.
march = 20 + leapJ - leapG
// Find how many years have passed since the last leap year.
if (jump - n < 6)
n = n - jump + div(jump + 4, 33) * 33
leap = mod(mod(n + 1, 33) - 1, 4)
if (leap === -1) {
leap = 4
}
return {
leap: leap
, gy: gy
, march: march
}
}
/*
Converts a date of the Jalaali calendar to the Julian Day number.
@param jy Jalaali year (1 to 3100)
@param jm Jalaali month (1 to 12)
@param jd Jalaali day (1 to 29/31)
@return Julian Day number
*/
function j2d(jy, jm, jd) {
var r = jalCal(jy)
return g2d(r.gy, 3, r.march) + (jm - 1) * 31 - div(jm, 7) * (jm - 7) + jd - 1
}
/*
Converts the Julian Day number to a date in the Jalaali calendar.
@param jdn Julian Day number
@return
jy: Jalaali year (1 to 3100)
jm: Jalaali month (1 to 12)
jd: Jalaali day (1 to 29/31)
*/
function d2j(jdn) {
var gy = d2g(jdn).gy // Calculate Gregorian year (gy).
, jy = gy - 621
, r = jalCal(jy)
, jdn1f = g2d(gy, 3, r.march)
, jd
, jm
, k
// Find number of days that passed since 1 Farvardin.
k = jdn - jdn1f
if (k >= 0) {
if (k <= 185) {
// The first 6 months.
jm = 1 + div(k, 31)
jd = mod(k, 31) + 1
return {
jy: jy
, jm: jm
, jd: jd
}
} else {
// The remaining months.
k -= 186
}
} else {
// Previous Jalaali year.
jy -= 1
k += 179
if (r.leap === 1)
k += 1
}
jm = 7 + div(k, 30)
jd = mod(k, 30) + 1
return {
jy: jy
, jm: jm
, jd: jd
}
}
/*
Calculates the Julian Day number from Gregorian or Julian
calendar dates. This integer number corresponds to the noon of
the date (i.e. 12 hours of Universal Time).
The procedure was tested to be good since 1 March, -100100 (of both
calendars) up to a few million years into the future.
@param gy Calendar year (years BC numbered 0, -1, -2, ...)
@param gm Calendar month (1 to 12)
@param gd Calendar day of the month (1 to 28/29/30/31)
@return Julian Day number
*/
function g2d(gy, gm, gd) {
var d = div((gy + div(gm - 8, 6) + 100100) * 1461, 4)
+ div(153 * mod(gm + 9, 12) + 2, 5)
+ gd - 34840408
d = d - div(div(gy + 100100 + div(gm - 8, 6), 100) * 3, 4) + 752
return d
}
/*
Calculates Gregorian and Julian calendar dates from the Julian Day number
(jdn) for the period since jdn=-34839655 (i.e. the year -100100 of both
calendars) to some millions years ahead of the present.
@param jdn Julian Day number
@return
gy: Calendar year (years BC numbered 0, -1, -2, ...)
gm: Calendar month (1 to 12)
gd: Calendar day of the month M (1 to 28/29/30/31)
*/
function d2g(jdn) {
var j
, i
, gd
, gm
, gy
j = 4 * jdn + 139361631
j = j + div(div(4 * jdn + 183187720, 146097) * 3, 4) * 4 - 3908
i = div(mod(j, 1461), 4) * 5 + 308
gd = div(mod(i, 153), 5) + 1
gm = mod(div(i, 153), 12) + 1
gy = div(j, 1461) - 100100 + div(8 - gm, 6)
return {
gy: gy
, gm: gm
, gd: gd
}
}
/*
Utility helper functions.
*/
function div(a, b) {
return ~~(a / b)
}
function mod(a, b) {
return a - ~~(a / b) * b
}
// Calculate Gregorian calendar date from Julian day
function jd_to_gregorian(jd) {
var res = d2g(jd);
return [res.gy, res.gm, res.gd];
}
function gregorian_to_jd(year, month, day) {
return g2d(year, month, day);
}
function jd_to_persian(jd) {
var res = d2j(jd);
return [res.jy, res.jm, res.jd];
}
// Determine Julian day from Persian date
function persian_to_jd(year, month, day) {
return j2d(year, month, day);
}
function persian_to_jd_fixed(year, month, day) {
/*
Fix `persian_to_jd` so we can use negative or large values for month, e.g:
persian_to_jd_fixed(1393, 26, 1) == persian_to_jd_fixed(1395, 2, 1)
persian_to_jd_fixed(1393, -2, 1) == persian_to_jd_fixed(1392, 10, 1)
*/
if (month > 12 || month <= 0) {
var yearDiff = Math.floor((month - 1) / 12);
year += yearDiff;
month = month - yearDiff * 12;
}
return persian_to_jd(year, month, day);
}
function digits_fa2en(value) {
var newValue = "";
for (var i = 0; i < value.length; i++) {
var ch = value.charCodeAt(i);
if (ch >= 1776 && ch <= 1785) // For Persian digits.
{
var newChar = ch - 1728;
newValue = newValue + String.fromCharCode(newChar);
}
else if (ch >= 1632 && ch <= 1641) // For Arabic & Unix digits.
{
var newChar = ch - 1584;
newValue = newValue + String.fromCharCode(newChar);
}
else
newValue = newValue + String.fromCharCode(ch);
}
return newValue;
}
function digits_en2fa(value) {
if (!value) {
return;
}
value = value + '';
var englishNumbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
persianNumbers = ["۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "۰"];
for (var i = 0, numbersLen = englishNumbers.length; i < numbersLen; i++) {
value = value.replace(new RegExp(englishNumbers[i], "g"), persianNumbers[i]);
}
return value;
}
function pad2(number) {
return number < 10 ? `0${number}` : number;
}
function parseDateString(string) {
var re = /^(شنبه|یکشنبه|دوشنبه|سه شنبه|چهارشنبه|پنجشنبه|جمعه)\s(\d\d)\s*(فروردین|اردیبهشت|خرداد|تیر|مرداد|شهریور|مهر|آبان|آذر|دی|بهمن|اسفند)\s*(\d\d\d\d)$/,
match = re.exec(string),
year,
month,
day;
if (!match) {
return;
}
year = parseInt(match[4]);
month = parseInt(JDate.i18n.months.indexOf(match[3])) + 1;
day = parseInt(match[2]);
var gd = jd_to_gregorian(persian_to_jd_fixed(year, month, day));
return new Date(gd[0], gd[1] - 1, gd[2]);
}
function parseDate(string, convertToPersian) {
/*
http://en.wikipedia.org/wiki/ISO_8601
http://dygraphs.com/date-formats.html
https://github.com/arshaw/xdate/blob/master/src/xdate.js#L414
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
tests:
+parseDate('2014') == +new Date('2014')
+parseDate('2014-2') == +new Date('2014-02')
+parseDate('2014-2-3') == +new Date('2014-02-03')
+parseDate('2014-02-03 12:11') == +new Date('2014/02/03 12:11')
+parseDate('2014-02-03T12:11') == +new Date('2014/02/03 12:11')
parseDate('2014/02/03T12:11') == undefined
+parseDate('2014/02/03 12:11:10.2') == +new Date('2014/02/03 12:11:10') + 200
+parseDate('2014/02/03 12:11:10.02') == +new Date('2014/02/03 12:11:10') + 20
parseDate('2014/02/03 12:11:10Z') == undefined
+parseDate('2014-02-03T12:11:10Z') == +new Date('2014-02-03T12:11:10Z')
+parseDate('2014-02-03T12:11:10+0000') == +new Date('2014-02-03T12:11:10Z')
+parseDate('2014-02-03T10:41:10+0130') == +new Date('2014-02-03T12:11:10Z')
*/
var re = /^(\d|\d\d|\d\d\d\d)(?:([-\/])(\d{1,2})(?:\2(\d|\d\d|\d\d\d\d))?)?(([ T])(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|([+-])(\d{2})(?::?(\d{2}))?)?)?$/;
var match = re.exec(string);
// re.exec('2012-4-5 01:23:10.1111+0130')
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
// ["2012-4-5 01:23:10.1111+0330", "2012", "-", "4", "5", " 01:23:10.1111+0130", " ", "01", "23", "10", "1111", "+0330", "+", "03", "30"]
var date;
var separator;
var timeSeparator;
var year;
var month;
var day;
var isISO;
var hour;
var minute;
var seconds;
var millis;
var tz;
var isNonLocal;
var tzOffset;
if (!match) {
return;
}
separator = match[2];
timeSeparator = match[6];
year = +match[1];
month = +match[3] || 1;
day = +match[4] || 1;
isISO = (separator !== '/') && (match[6] !== ' ');
hour = +match[7] || 0;
minute = +match[8] || 0;
seconds = +match[9] || 0;
millis = +(`0.${(match[10] || '0')}`) * 1000;
tz = match[11];
isNonLocal = isISO && (tz || !match[5]);
tzOffset = (match[12] === '-' ? -1 : 1) * ((+match[13] || 0) * 60 + (+match[14] || 0));
// timezone should be empty if dates are with / (2012/1/10)
if ((tz || timeSeparator === 'T') && !isISO) {
return;
}
// one and only-one of year/day should be 4-chars (2012/1/10 vs 10/1/2012)
if ((day >= 1000) === (year >= 1000)) {
return;
}
if (day >= 1000) {
// year and day only can be swapped if using '/' as separator
if (separator === '-') {
return;
}
day = +match[1];
year = day;
}
if (convertToPersian) {
const persian = jd_to_gregorian(persian_to_jd_fixed(year, month, day));
year = persian[0];
month = persian[1];
day = persian[2];
}
date = new Date(year, month - 1, day, hour, minute, seconds, millis);
if (isNonLocal) {
date.setUTCMinutes(date.getUTCMinutes() - date.getTimezoneOffset() + tzOffset);
}
return date;
}
var isDate = function (obj) {
return (/Date/).test(Object.prototype.toString.call(obj)) && !isNaN(obj.getTime());
}
var Data = window.Date;
var proto;
/**
* @param {Object=} a ,may have different types for different semantics: 1) string: parse a date
* 2) Date object: clone a date object 3) number: value for year
* @param {Number=} month
* @param {Number=} day
* @param {Number=} hour
* @param {Number=} minute
* @param {Number=} second
* @param {Number=} millisecond
* @constructor
* @extends {Date}
*/
function JDate(a, month, day, hour, minute, second, millisecond) {
if (a === '') {
this._d = new Date();
}
else if (typeof a === 'string' && (parseInt(a) != a)) {
a = digits_fa2en(a);
this._d = parseDate(a, true);
if (!isDate(this._d)) {
this._d = parseDateString(a);
if (!isDate(this._d)) {
this._d = new Date(a);
if (!isDate(this._d)) {
this._d = new Date();
}
}
}
} else if (arguments.length === 0) {
this._d = new Date();
} else if (arguments.length === 1) {
this._d = new Date((a instanceof JDate) ? a._d : a);
} else {
const persian = jd_to_gregorian(persian_to_jd_fixed(parseInt(a), (parseInt(month) || 0) + 1, parseInt(day) || 1));
this._d = new Date(persian[0], persian[1] - 1, persian[2], hour || 0, minute || 0, second || 0, millisecond || 0);
}
this._date = this._d;
this._cached_date_ts = null;
this._cached_date = [0, 0, 0];
this._cached_utc_date_ts = null;
this._cached_utc_date = [0, 0, 0];
}
proto = JDate.prototype;
/**
* returns current Jalali date representation of internal date object, eg. [1394, 12, 5]
* Caches the converted Jalali date for improving performance
* @returns {Array}
*/
proto._persianDate = function () {
if (this._cached_date_ts != +this._d) {
this._cached_date_ts = +this._d;
this._cached_date = jd_to_persian(gregorian_to_jd(this._d.getFullYear(), this._d.getMonth() + 1, this._d.getDate()));
}
return this._cached_date;
};
/**
* Exactly like `_persianDate` but for UTC value of date
*/
proto._persianUTCDate = function () {
if (this._cached_utc_date_ts != +this._d) {
this._cached_utc_date_ts = +this._d;
this._cached_utc_date = jd_to_persian(gregorian_to_jd(this._d.getUTCFullYear(), this._d.getUTCMonth() + 1, this._d.getUTCDate()));
}
return this._cached_utc_date;
};
/**
*
* @param which , which component of date to change? 0 for year, 1 for month, 2 for day
* @param value , value of specified component
* @param {Number=} dayValue , change the day along-side specified component, used for setMonth(month[, dayValue])
*/
proto._setPersianDate = function (which, value, dayValue) {
var persian = this._persianDate();
persian[which] = value;
if (dayValue !== undefined) {
persian[2] = dayValue;
}
var new_date = jd_to_gregorian(persian_to_jd_fixed(persian[0], persian[1], persian[2]));
this._d.setFullYear(new_date[0]);
this._d.setMonth(new_date[1] - 1, new_date[2]);
};
/**
* Exactly like `_setPersianDate`, but operates UTC value
*/
proto._setUTCPersianDate = function (which, value, dayValue) {
var persian = this._persianUTCDate();
if (dayValue !== undefined) {
persian[2] = dayValue;
}
persian[which] = value;
var new_date = jd_to_gregorian(persian_to_jd_fixed(persian[0], persian[1], persian[2]));
this._d.setUTCFullYear(new_date[0]);
this._d.setUTCMonth(new_date[1] - 1, new_date[2]);
};
// All date getter methods
proto.getDate = function () {
return this._persianDate()[2];
};
proto.getMonth = function () {
return this._persianDate()[1] - 1;
};
proto.getFullYear = function () {
return this._persianDate()[0];
};
proto.getUTCDate = function () {
return this._persianUTCDate()[2];
};
proto.getUTCMonth = function () {
return this._persianUTCDate()[1] - 1;
};
proto.getUTCFullYear = function () {
return this._persianUTCDate()[0];
};
// All date setter methods
proto.setDate = function (dayValue) {
this._setPersianDate(2, dayValue);
};
proto.setFullYear = function (yearValue) {
this._setPersianDate(0, yearValue);
};
proto.setMonth = function (monthValue, dayValue) {
this._setPersianDate(1, monthValue + 1, dayValue);
};
proto.setUTCDate = function (dayValue) {
this._setUTCPersianDate(2, dayValue);
};
proto.setUTCFullYear = function (yearValue) {
this._setUTCPersianDate(0, yearValue);
};
proto.setUTCMonth = function (monthValue, dayValue) {
this._setUTCPersianDate(1, monthValue + 1, dayValue);
};
/**
* The Date.toLocaleString() method can return a string with a language sensitive representation of this date,
* so we change it to return date in Jalali calendar
*/
proto.toLocaleString = function () {
return `${this.getFullYear()}/${pad2(this.getMonth() + 1)}/${pad2(this.getDate())} ${pad2(this.getHours())}:${pad2(this.getMinutes())}:${pad2(this.getSeconds())}`;
};
/**
* The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.
*/
JDate.now = function now() {
return Date.now();
};
/**
* parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
*/
JDate.parse = function parse(strDate) {
return (new JDate(strDate)).getTime();
};
proto.toDateString = function () {
return JDate.i18n.weekdays[this.getDay()] + ' '
+ pad2(this.getDate()) + ' '
+ JDate.i18n.months[this.getMonth()] + ' ' + this.getFullYear();
}
JDate.i18n = {
months: ['فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'],
weekdays: ['یکشنبه', 'دوشنبه', 'سه شنبه', 'چهارشنبه', 'پنجشنبه', 'جمعه', 'شنبه'],
weekdaysShort: ['یک', 'دو', 'سه', 'چهار', 'پنج', 'جمعه', 'شنبه']
}
/**
* The Date.UTC() method accepts the same parameters as the longest form of the constructor, and returns the number of
* milliseconds in a Date object since January 1, 1970, 00:00:00, universal time.
*/
JDate.UTC = function (year, month, date, hours, minutes, seconds, milliseconds) {
var d = jd_to_gregorian(persian_to_jd_fixed(year, month + 1, date || 1));
return Date.UTC(d[0], d[1] - 1, d[2], hours || 0, minutes || 0, seconds || 0, milliseconds || 0);
};
// Proxy all time-related methods to internal date object
['getHours', 'getMilliseconds', 'getMinutes', 'getSeconds', 'getTime', 'getUTCDay',
'getUTCHours', 'getTimezoneOffset', 'getUTCMilliseconds', 'getUTCMinutes', 'getUTCSeconds',
'setHours', 'setMilliseconds', 'setMinutes', 'setSeconds', 'setTime', 'setUTCHours',
'setUTCMilliseconds', 'setUTCMinutes', 'setUTCSeconds', 'toISOString',
'toJSON', 'toString', 'toLocaleDateString', 'toLocaleTimeString', 'toTimeString',
'toUTCString', 'valueOf', 'getDay'].forEach((method) => {
proto[method] = function () {
return this._d[method].apply(this._d, arguments);
};
});
/*!
* Pikaday
*
* Copyright © 2014 David Bushell | BSD & MIT license | https://github.com/dbushell/Pikaday
*/
(function (root, factory)
{
'use strict';
var moment;
if (typeof exports === 'object') {
// CommonJS module
// Load moment.js as an optional dependency
try { moment = require('moment'); } catch (e) {}
module.exports = factory(moment);
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(function (req)
{
// Load moment.js as an optional dependency
var id = 'moment';
try { moment = req(id); } catch (e) {}
return factory(moment);
});
} else {
root.Pikaday = factory(root.moment);
}
}(this, function (moment)
{
return function(Date,oDate){
'use strict';
/**
* feature detection and helper functions
*/
var hasMoment = typeof moment === 'function',
hasEventListeners = !!window.addEventListener,
document = window.document,
sto = window.setTimeout,
addEvent = function(el, e, callback, capture)
{
if (hasEventListeners) {
el.addEventListener(e, callback, !!capture);
} else {
el.attachEvent('on' + e, callback);
}
},
removeEvent = function(el, e, callback, capture)
{
if (hasEventListeners) {
el.removeEventListener(e, callback, !!capture);
} else {
el.detachEvent('on' + e, callback);
}
},
fireEvent = function(el, eventName, data)
{
var ev;
if (document.createEvent) {
ev = document.createEvent('HTMLEvents');
ev.initEvent(eventName, true, false);
ev = extend(ev, data);
el.dispatchEvent(ev);
} else if (document.createEventObject) {
ev = document.createEventObject();
ev = extend(ev, data);
el.fireEvent('on' + eventName, ev);
}
},
trim = function(str)
{
return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g,'');
},
hasClass = function(el, cn)
{
return (' ' + el.className + ' ').indexOf(' ' + cn + ' ') !== -1;
},
addClass = function(el, cn)
{
if (!hasClass(el, cn)) {
el.className = (el.className === '') ? cn : el.className + ' ' + cn;
}
},
removeClass = function(el, cn)
{
el.className = trim((' ' + el.className + ' ').replace(' ' + cn + ' ', ' '));
},
isArray = function(obj)
{
return (/Array/).test(Object.prototype.toString.call(obj));
},
isDate = function(obj)
{
return ((/Object/).test(Object.prototype.toString.call(obj)) && typeof obj.getTime === "function" && !isNaN(obj.getTime())) ||
((/Date/).test(Object.prototype.toString.call(obj)) && !isNaN(obj.getTime()));
},
isODate = function(obj){
return (/Date/).test(Object.prototype.toString.call(obj)) && !isNaN(obj.getTime());
},
isWeekend = function(date)
{
var day = date.getDay();
return day === 4 || day === 5;
},
isLeapYear = function(year)
{
return jalCal(year).leap === 0
},
getDaysInMonth = function(year, month)
{
return [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, isLeapYear(year,true) ? 30 : 29][month];
},
setToStartOfDay = function(date)
{
if (isDate(date)) date.setHours(0,0,0,0);
},
compareDates = function(a,b)
{
// weak date comparison (use setToStartOfDay(date) to ensure correct result)
return a.getTime() === b.getTime();
},
extend = function(to, from, overwrite)
{
var prop, hasProp;
for (prop in from) {
hasProp = to[prop] !== undefined;
if (hasProp && typeof from[prop] === 'object' && from[prop] !== null && from[prop].nodeName === undefined) {
if (isDate(from[prop])) {
if (overwrite) {
to[prop] = new Date(from[prop].getTime());
}
}
else if (isArray(from[prop])) {
if (overwrite) {
to[prop] = from[prop].slice(0);
}
} else {
to[prop] = extend({}, from[prop], overwrite);
}
} else if (overwrite || !hasProp) {
to[prop] = from[prop];
}
}
return to;
},
adjustCalendar = function(calendar) {
if (calendar.month < 0) {
calendar.year -= Math.ceil(Math.abs(calendar.month)/12);
calendar.month += 12;
}
if (calendar.month > 11) {
calendar.year += Math.floor(Math.abs(calendar.month)/12);
calendar.month -= 12;
}
return calendar;
},
/**
* defaults and localisation
*/
defaults = {
// bind the picker to a form field
field: null,
// automatically show/hide the picker on `field` focus (default `true` if `field` is set)
bound: undefined,
// position of the datepicker, relative to the field (default to bottom & left)
// ('bottom' & 'left' keywords are not used, 'top' & 'right' are modifier on the bottom/left position)
position: 'bottom left',
// automatically fit in the viewport even if it means repositioning from the position option
reposition: true,
// the default output format for `.toString()` and `field` value
format: 'YYYY-MM-DD',
// the initial date to view when first opened
defaultDate: null,
// make the `defaultDate` the initial selected value
setDefaultDate: false,
// first day of week (0: Sunday, 1: Monday etc)
firstDay: 0,
// the default flag for moment's strict date parsing
formatStrict: false,
// the minimum/earliest date that can be selected
minDate: null,
// the maximum/latest date that can be selected
maxDate: null,
// number of years either side, or array of upper/lower range
yearRange: 10,
// show week numbers at head of row
showWeekNumber: false,
// used internally (don't config outside)
minYear: 0,
maxYear: 9999,
minMonth: undefined,
maxMonth: undefined,
startRange: null,
endRange: null,
isRTL: true,
persianNumbers: true,
// Additional text to append to the year in the calendar title
yearSuffix: '',
// Render the month after year in the calendar title
showMonthAfterYear: false,
// Render days of the calendar grid that fall in the next or previous month
showDaysInNextAndPreviousMonths: false,
// how many months are visible
numberOfMonths: 1,
// when numberOfMonths is used, this will help you to choose where the main calendar will be (default `left`, can be set to `right`)
// only used for the first display or when a selected date is not visible
mainCalendar: 'left',
// Specify a DOM element to render the calendar in
container: undefined,
// internationalization
i18n: {
previousMonth: 'ماه قبل',
nextMonth: 'ماه بعد',
months: ['فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'],
weekdays: ['یکشنبه', 'دوشنبه', 'سه شنبه', 'چهارشنبه', 'پنجشنبه', 'جمعه', 'شنبه'],
weekdaysShort: ['یک', 'دو', 'سه', 'چهار', 'پنج', 'جمعه', 'شنبه']
},
// Theme Classname
theme: null,
// callback function
onSelect: null,
onOpen: null,
onClose: null,
onDraw: null
},
/**
* templating functions to abstract HTML rendering
*/
renderDayName = function(opts, day, abbr)
{
day += opts.firstDay;
while (day >= 7) {
day -= 7;
}
return abbr ? opts.i18n.weekdaysShort[day] : opts.i18n.weekdays[day];
},
renderDay = function(opts)
{
var arr = [];
var ariaSelected = 'false';
if (opts.isEmpty) {
if (opts.showDaysInNextAndPreviousMonths) {
arr.push('is-outside-current-month');
} else {
return '<td class="is-empty"></td>';
}
}
if (opts.isDisabled) {
arr.push('is-disabled');
}
if (opts.isToday) {
arr.push('is-today');
}
if (opts.isSelected) {
arr.push('is-selected');
ariaSelected = 'true';
}
if (opts.isInRange) {
arr.push('is-inrange');
}
if (opts.isStartRange) {
arr.push('is-startrange');
}
if (opts.isEndRange) {
arr.push('is-endrange');
}
return '<td data-day="' + opts.day + '" class="' + arr.join(' ') + '" aria-selected="' + ariaSelected + '">' +
'<button class="pika-button pika-day" type="button" ' +
'data-pika-year="' + opts.year + '" data-pika-month="' + opts.month + '" data-pika-day="' + opts.day + '">' +
(opts.persianNumbers? digits_en2fa(opts.day) : opts.day)+
'</button>' +
'</td>';
},
renderWeek = function (d, m, y, persianNumbers) {
// Lifted from http://javascript.about.com/library/blweekyear.htm, lightly modified.
var onejan = new Date(y, 0, 1),
weekNum = Math.ceil((((new Date(y, m, d) - onejan) / 86400000) + onejan.getDay()+1)/7);
return '<td class="pika-week">' + (persianNumbers? digits_en2fa(weekNum) : weekNum) + '</td>';
},
renderRow = function(days, isRTL)
{
return '<tr>' + (isRTL ? days.reverse() : days).join('') + '</tr>';
},
renderBody = function(rows)
{
return '<tbody>' + rows.join('') + '</tbody>';
},
renderHead = function(opts)
{
var i, arr = [];
if (opts.showWeekNumber) {
arr.push('<th></th>');
}
for (i = 0; i < 7; i++) {
arr.push('<th scope="col"><abbr title="' + renderDayName(opts, i) + '">' + renderDayName(opts, i, true) + '</abbr></th>');
}
return '<thead><tr>' + (opts.isRTL ? arr.reverse() : arr).join('') + '</tr></thead>';
},
renderTitle = function(instance, c, year, month, refYear, randId)
{
var i, j, arr,
opts = instance._o,
isMinYear = year === opts.minYear,
isMaxYear = year === opts.maxYear,
html = '<div id="' + randId + '" class="pika-title" role="heading" aria-live="assertive">',
monthHtml,
yearHtml,
prev = true,
next = true;
for (arr = [], i = 0; i < 12; i++) {
arr.push('<option value="' + (year === refYear ? i - c : 12 + i - c) + '"' +
(i === month ? ' selected="selected"': '') +
((isMinYear && i < opts.minMonth) || (isMaxYear && i > opts.maxMonth) ? 'disabled="disabled"' : '') + '>' +
opts.i18n.months[i] + '</option>');
}
monthHtml = '<div class="pika-label">' + opts.i18n.months[month] + '<select class="pika-select pika-select-month" tabindex="-1">' + arr.join('') + '</select></div>';