-
Notifications
You must be signed in to change notification settings - Fork 0
/
EZdate.deleteme.js
1285 lines (1164 loc) · 43.4 KB
/
EZdate.deleteme.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
/*--------------------------------------------------------------------------------------------------
DW lint options -- function below not called
--------------------------------------------------------------------------------------------------*/
/*global EZ, e:true */
var e;
(function jshint_globals_not_used() {e = [e]}); //global variables / functions defined but not used
/*---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------*/
EZ.options.date = {
debug: true,
baseDate: new Date('1/1/1970'),
dateString: "smart", //false or blank for native;
//"dashes" to only convert dashes in dates to slashs
//"smart" for dashes and more formats -- see createDate()
//e.g. 2016-01-12, 2-28, 12-1999
//default blank to now -- native Date() returns invalid date
asNow: ["''"], //typeof(dateString) treated as new Date() i.e. now
asNull: [undefined], //"undefined object function false",
//typeof(dateString) treated as new Date(null) i.e. baseDate
//vs Invalid Date -- both new Date(null) and Invalid Date
//are less than any valid date
invalidDate: null, //"null" to return Invalid Date as null
//allows if (!date)... vs if (date == false)...
callback: false, //false ignores EZ.date.prototype..._callback functions
//planned for RZdate backward compatibility
//option added as development convenience but left
override: false, //prototype can override EZ.date() nested functions -- value??
formats: null, //formats added to new EZ.date(...) -- set to defaultFormats
//1st time EZ.date() called with any arguments
//use $setFormats() or $removeFormats() to change global or
//individual date instance values.
defaultFormats:
{
//default format //12 hr clock with am/pm drops time if 0
'*': 'mm-dd-yyyy hh:MM:tt.nnn ampm', //drops seconds and milliseconds if both 0
//dates -- all formats blank if no date component (i.e. date <= 1/1/1970)
d: 'd', //2 digit day of month
dd: 'dd', //2 digit day of month
da: "da", //3 char day of week e.g. "Sun"
day: "dayName", //full day of Week e.g. "Sunday"
daySuffix: "daySuffix", //day of month with suffix e.g. 1st, 2nd 9th
m: 'm', //1 or 2 digit day of month: 1, 2 ... 12
mm: 'mm', //2 digit day of month: 01, 02 ... 12
mo: "mo", //3 char month name e.g. "Jan"
month: "monthName", //full month name e.g. "January"
yr: "yy", //2 digit year
yy: "yy", //2 digit year
year: "yyyy", //4 digit year
yyyy: "yyyy", //4 digit year
dateBrief: 'mm-dd-yy', //2 digits for month, day & year
dateShort: 'mm-dd-yyyy', //2 digits for month & day; 4 digit year
dateLong: 'day month daySuffix, yyyy', //full day of week, month name, day of month with suffix
// 4 digit year: e.g. Sunday January 3rd, 2016
// e.g. Monday January
dateSmart: 'da mo d, yyyy', //3 char day of week and month name, 1 or 2 digit day
// of month, 4 digit year: e.g. Sun Jan 3, 2016
// tip: use substr(4) to omit day of week
//times
ampm: "ampm", //either "am" or "pm" if date has time part
AMPM: "AMPM", //always "AM" or "PM"
HR: 'HR', //always 1 or 2 digit hour (24 hour clock): 1 ... 23
hr: 'h', //always 1 or 2 digit hour (12 hour clock): 0 ... 12
min: 'M', //always 1 or 2 digit seconds: 0 ... 59
sec: 's', //always 1 or 2 digit seconds: 0 ... 59
h: "h", //1 or 2 digit hour if > 0 (12 hour clock): 1 ... 12
hh: 'hh', //2 digit hour (12 hour clock): 00, 02 ... 12
H: "H", //1 or 2 digit hour if > 0 (24 hour clock): 01 ... 23
HH: 'HH', //always 2 digit hour (24 hour clock): 00 ... 23
M: "M", //1 or 2 digit minutes if > 0: 1 ... 59
MM: 'MM', //always 2 digit minutes: 0 ... 59
s: "s", //1 or 2 digit seconds if > 0 : 0 ... 59
ss: 'ss', //2 digit seconds if > 0 : 1 ... 59
S: 'S', //always 1 or 2 digit seconds: 0, 1 ... 59
SS: 'SS', //always 2 digit seconds: 00, 01 ... 59
t: "t", //1 or 2 digit seconds if > 0 or millisecond > 0
tt: 'tt', //2 digit if seconds if > 0 or millisecond > 0
nnn: "nnn", //3 digit millisecond with "." prexix if > 0 else blank
NNN: "NNN", //always 3 digit millisecond no prexix
noon: "noon", //12 hour clock; blank if time part is 0 otherwise
// 1 or 2 digit hr, 2 digit minutes, no seconds
// "noon" if 12:00 else am or pm suffix
// e.g. "1:00 am", "noon" "12:01 pm", "11:59 pm"
NOON: "NOON",
midnight: "midnight", //same as noon except "midnight" if time part is 0
MIDNIGHT: "MIDNIGHT",
zone: "zone", //3 char uppercase time zone: e.g. "EDT"
zoneFull: "zoneFull", //time zone full words: "Eastern Daylight Time"
//if browser returns full words
//compound times: built from one or more of above time formats
// leading and trailing spaces or non-numeric, non-alpha characters TRIMMED after
// all above base formats applied except as noted below.
// multiple space or any other non-numeric, non-alpha break characters collapsed
// to single occurance except as noted below.
//
//EXCEPTIONS:
// noon or NOON retain blank or prefix respectivly when timepart is not 0
// midnight OR MIDNIGHT ALWAYS retains blank or prefix
time: 'H:MM:ss ampm', //H:MM:ss ampm ??same as long except seconds dropped if 0
seconds: "s.nnn", //e.g. "0", "0.001", "10.100"
milliseconds: "s.NNN", //e.g. "0.000", "1.001", 10.100"
//does not use noon or midnight
timeBrief: 'h:MM ampm', //1 or 2 digits for hr; seconds always dropped
//blank if timePart() == 0
//Noon for 12:00:00; 12 hr clock
timeFull: 'HH:MM:ss.nnn', //always 2 digits for hour; seconds dropped if 0
//24 hour clock (hours are 00-23)
timeLong: 'midnight:SS ampm', //1 or 2 digits for hr; seconds always displayed
//Noon for 12:00:00; Midnight for 00:00:00
timeMini: 'midnight:ss ampm', //same as long except seconds dropped if 0
//Noon for 12:00:00; Midnight for 00:00:00
timeShort: 'HH:MM:ss', //always 2 digits for hour; seconds dropped if 0
//24 hour clock (hours are 00-23)
timeSmart: 'noon:ss ampm', //blank if no timePart(); single digit hr padded
//Noon for 12:00:00; seconds dropped if 0
//date time
dateTimeBrief: 'mm-dd-yy noon', //time dropped if timePart() = 0
//Noon for 12:00; seconds always dropped
dateTimeShort: 'mm-dd-yyyy HH:MM:SS', //time always displayed with seconds
//24 hour clock
dateTimeLong: 'da mo d, yyyy hh:MM:ss', //time dropped if timePart() = 0
//Noon for 12:00:00
dateTimeSmart: 'd, yyyy h:MM:ss ampm', //time dropped if timePart() = 0
//Noon for 12:00:00
dateTimeFull: 'mm-dd-yyyy HH:MM:SS.NNN', //24 hour clock always show time with milliseconds
dateTimeAll: 'mm-dd-yyyy HH:MM:SS.NNN zone', //24 hour clock time with milliseconds and time zone
//timestamps
timestamp: 'yyyy-mm-dd.HH:MM:SS', //HH is 24 hour always keeps time and seconds
timestampDate: 'yyyy-mm-dd', //date only never time -- legacy: revizeDate
timestampSmart: 'yyyy-mm-dd HH:ss:tt.nnn', //time dropped if timePart() = 0 -- legacy: revizeDate
//HH is 24 hour time; seconds dropped if 0
timestampShort: 'yyyy-mm-dd HH:MM', //always keeps time without seconds
timestampFull: 'yyyy-mm-dd HH:MM:SS.NNN', //always keeps milliseconds
timestamp_filename: 'yyyy-mm-dd.HH~MM~SS',
timestampShort_filename: 'yyyy-mm-dd.HH~MM',
timestampSmart_filename: 'yyyy-mm-dd.HH~MM~ss',
timestampFull_filename: 'yyyy-mm-dd.HH~MM~SS~NNN'
}
}
/*---------------------------------------------------------------------------------------------
Extend Date class with additional properties and methods.
Smarter handling of Date String argument to recognize more formats -- most notably dashes by
Firefox and Chrome -- see createDate() for more details.
*
* for string argument, convert dashes --> slash and change several
* un-recognized date formats to dates recognizable by native Date()
Smarter to supports dashes "-" in constructor for FF & Chrome:
.e.g. new EZ.date('12-21-2012')
Plus
The builtin Date() object is not extended to avoid different behavior caused
by adding prototypes directly.
---------------------------------------------------------------------------------------------*/
EZ.date = function EZdate(options)
{
var options = getDateOptions(this, arguments);
if (options.message)
return options.message;
var rtnValue = options.rtnValue;
///if(EZ.start(options)))
{
rtnValue = options.fnRun.apply(options.ctx, options.args);
var callback = options.callback[options.fn];
if (callback)
rtnValue = callback.call(options.ctx, rtnValue);
}
//=====================
return rtnValue
/// return EZ.finish(rtnValue);
//=====================
/**
* returns date using specified format(s) or default if none found
*/
function _toString(args)
{
var formattedDate = '';
try
{
if (updateFormattedDate(this)) //update _formattedDate property
{
var formats = this.getFormats();
args.forEach(function(arg)
{
EZ.toArray(arg).forEach(function(fmt) //for each format . . .
{ //treat as literal if unknown fmt
formattedDate += formats[fmt] ? this._formattedDate[fmt] : fmt;
});
});
return formattedDate !== '' || this._formattedDate['*'];
}
}
catch (e)
{
void(0);
}
return formattedDate || new Date(this).toString();
}
//____________________________________________________________________________________________
/**
* function updateFormattedDate(date)
*
* _formattedDate date property updated with all above date formats -- toString() just looks up.
*
* exposed so all formats are displayed by JavaScript debugger but should not be referenced
* directly to avoid js error if format name misspelled or names are changed in future.
*
* e.g. date.toString('shortDate') -NOT- date._formattedDate.shortDate
*
* If names are changed or deprecated, backward compatibility will be provided via toString()
*/
function updateFormattedDate(date)
{
if (!date.isEZ || !EZ.options.date.formats)
return false;
var dateTime = date.getTime(); //remember value formatted
if (date._formattedDate && date._dateTime == dateTime)
return true;
if (EZ.options.date.debug) console.clear();
date._dateString = new Date(dateTime) + '';
var dt = {
day: date.getDate(),
dayName: date.getDayName(),
month: date.getMonth(),
year: date.getFullYear(),
monthName: date.getMonthName(),
hours: date.getHours(),
minutes: date.getMinutes(),
seconds: date.getSeconds(),
milliseconds: date.getMilliseconds()
}
dt.da = dt.dayName.substr(0,3);
dt.mo = dt.monthName.substr(0,3);
//----------------------\\
//----- date formats -----\\
//--------------------------\\
dt.yyyy = dt.year + '';
dt.yy = dt.yyyy.right(2);
dt.mm = ('0'+(dt.month+1)).right(2);
dt.m = dt.month + '';
dt.dd = ('0'+dt.day).right(2);
dt.d = dt.day + '';
dt.daySuffix = dt.day.addSuffix();
if (date.getTime() <= EZ.options.date.baseDate.getTime())
dt.year = dt.mm = dt.m = dt.dd = dt.d = ''; //clear date formats if only time
//----------------------\\
//----- time formats -----\\
//--------------------------\\
dt.zoneFull = date._dateString.matchPlus(/\(([A-Za-z\s].*)\)/)[1];
dt.zone = dt.zoneFull.replace(/[a-z\W]/g, '');
dt.HR = dt.hours + '';
dt.H = dt.hours === 0 ? '' : dt.HR;
dt.HH = ('0' + dt.hours).right(2);
dt.h = dt.hours === 0 ? ''
: dt.hours > 12 ? dt.hours - 12
: dt.hours + '';
dt.hh = dt.hours ? ('0'+ dt.h).right(2): '';
dt.MM = ('0' + dt.minutes).right(2);
dt.M = dt.minutes + '';
dt.SS = ('0' + dt.seconds).right(2);
dt.S = dt.seconds + '';
dt.ss = dt.seconds ? dt.SS : ''
dt.s = dt.seconds ? dt.S : ''
dt.t = (dt.seconds + dt.milliseconds) ? dt.S : '';
dt.tt = (dt.seconds + dt.milliseconds) ? dt.SS : '';
dt.NNN = ('000' + dt.milliseconds).right(3);
dt.nnn = dt.milliseconds ? dt.NNN : '';
dt.ampm = dt.AMPM = (dt.hours < 12) ? 'am' : 'pm';
dt.noon = dt.midnight = dt.HH + ':' + dt.MM + ' ' + dt.AMPM;
dt.NOON = dt.MIDNIGHT = dt.HH + ':' + dt.MM + ':' + dt.SS + ' ' + dt.AMPM;
dt.noon = dt.midnight = dt.noon.replace(/12:00 pm/i,'noon');
dt.NOON = dt.MIDNIGHT = dt.noon.replace(/12:00:00 pm/i,'noon');
dt.noon = dt.noon.replace(/12:00 am/i,'');
dt.NOON = dt.NOON.replace(/12:00:00 am/i,'');
dt.midnight = dt.midnight.replace(/12:00 am/i,'midnight');
dt.MIDNIGHT = dt.MIDNIGHT.replace(/12:00:00 am/i,'midnight');
//--------------------------------\\
//----- create formatted dates -----\\
//------------------------------------\\
var options = EZ.options.date;
var formats = {}
var keys = Object.keys(options.defaultFormats).sort(sortByLength);
keys.forEach(function(key) //---------------------------
{ //set all base formats values
var value = options.defaultFormats[key]; //---------------------------
if (dt[value] == null)
return;
formats[key] = dt[value] + ''; //no expressions
if (dt[value] == null)
dt[value] = value;
});
e = logFormats('baseFormats');
for (var key in options.defaultFormats) //----------------------------
{ //set remaining defaultFormats
var value = options.defaultFormats[key]; //----------------------------
if (dt[value] != null) continue; //skip if base format
formats[key] = setFormat(key,value); //apply all key/values & expr
dt[formats[key]] = formats[key];
}
e = logFormats('defaultFormats');
var dateFormats = date.getFormats(); //get format list for this date
var formatList = Object.keys(dateFormats);
formatList.remove(Object.keys(formats)).forEach(function(key)
{ //-------------------------
if (formats.includes(key)) return; //set custom formats if any
var value = dateFormats[key]; //-------------------------
formats[key] = setFormat(key, value);
});
e = logFormats('custom Formats');
date._formattedDate = {}; //recreate _formattedDate property
formatList.forEach(function(key) //-------------------------
{ //copy formats in date list
var value = getFormat(formats[key]); //--------------------------
if (key.toLowerCase() != 'ampm' && /^(am|pm)$/i.test(value.trim()))
value = '';
date._formattedDate[key] = value;
});
e = logFormats('savedFormats', date._formattedDate);
date._dateTime = dateTime;
//======================
return true;
//======================
/**
* 1. unescape format replacements made by setFormat()
* 2. trim leading and trailing spaces or non-numeric, non-alpha characters
* 3. collapse multiple spaces or any other non-numeric, non-alpha break char
* to first character.
* 4. replace "^" or "&" placeholder at start of format with space or " "
* respectively.
*/
function getFormat(value)
{
if (typeof(value) != 'string')
return value;
//value = value.replace(/-@-/g, '');
value = value.replace(/([^\w\d^& ]{2,})/g, function(all,dups)
{
return dups.substr(0,1)
});
value = value.replace(/([^\w\d,]\s+)/, function() {return ' '} );
value = value.trim().trimPlus(/[^\w\d^&)]/);
value = value.replace(/^\^/, ' ').replace(/^&/, ' ');
/*TODO:
*/
return value;
}
/**
* replace references in value to other format keys.
*/
function setFormat(key,value)
{
var isReplaced = false;
var keys = Object.keys(dt); //.sort(sortByLength);
keys.remove(key).forEach(function(fmt)
{
if (fmt == '*') return;
var regex = RegExp( '(\\b' + fmt + '\\b)', 'g');
value = value.replace(regex, function()
{
isReplaced = true;
return 'dt' + fmt + 'k' + dt[fmt] + 'v';
})
});
if (isReplaced)
{
var val = value.replace(/dt.*?k(.*?)v/g, '$1');
try
{
if (false)
{
eval('value='+value);
dt[key] = value;
}
else value = val;
}
catch (e)
{
value = val;
}
}
//======================
return value;
//======================
}
/**
* sort Array of keys by length of key.
*/
function sortByLength(a, b)
{
return (a.length > b.length) ? -1
: (a.length < b.length) ? 1
: 0;
}
/**
* debugging
*/
function logFormats(note, fmt)
{
if (!EZ.options.date.debug) return;
fmt = fmt || formats
var formatted = {};
for (var key in fmt) //append any formats in date list
formatted[key] = getFormat(fmt[key]);
console.log({note:note, count: Object.keys(formatted).length, formats: formatted});
return formatted;
}
}
//____________________________________________________________________________________________
/**
* createDate(year,month,day,hours,minutes,seconds,ms)
*
* Create new Date() Object with additional properties and functions.
* saves default toString() as toStringDefault() -- also accessible with toString('default')
*/
function createDate(year,month,day,hours,minutes,seconds,ms)
{
var date;
switch (arguments.length)
{
case 0:
date = new Date();
break;
case 1:
date = getDate(year);
break;
case 2:
date = new Date(year,month);
break;
case 3:
date = new Date(year,month,day);
break;
case 4:
date = new Date(year,month,day,hours);
break;
case 5:
date = new Date(year,month,day,hours,minutes);
break;
case 6:
date = new Date(year,month,day,hours,minutes,seconds);
break;
default:
date = new Date(year,month,day,hours,minutes,seconds,ms);
break;
}
if (isNaN(date)) // && EZ.date.settings.nullForInvalidDate)
return null; //return null for invalid date
date._options = EZ.mergeAll(EZ.options.date); //init date._options
setProperties(date) //append other properties and functions
date.isEZ = true;
updateFormattedDate(date);
//=======================================
return options.ctx = options.date = date;
//=======================================
//____________________________________________________________________________________________
/**
* Smarter handling of Date String argument to recognize more formats -- most notably dashes.
*
* for string argument, convert dashes --> slash and change several
* un-recognized date formats to dates recognizable by native Date()
*/
function getDate(dateStr)
{
switch (getType(dateStr))
{
case 'String':
{
if (dateStr === '') //if blank string, lookup override default
{
return EZ.options.date.asNow.includes('') ? new Date()
: EZ.options.date.asNull.includes('') ? new Date(null)
: new Date(''); //invalid
}
break;
}
case 'Undefined': //treat undefined as null ??
{
return EZ.options.date.asNow.includes(undefined) ? new Date()
: EZ.options.date.asNull.includes(undefined) ? new Date(null)
: new Date(''); //invalid
}
case 'Null': //resolves as 12-31-1969 -- less than any real dates
case 'Boolean': //treated same as native -- resolves to now
case 'Number':
case 'Date':
return new Date(dateStr);
default:
return new Date(dateStr);
}
dateStr = dateStr.replace(/Noon/i,'12:00pm');
dateStr = dateStr.replace(/Midnight/i,'12:00am');
dateStr = dateStr.replace(/(\d)\-(\d)/g,'$1/$2');
dateStr = dateStr.replace(/(\d{1,2}\/\d{1,2}\/)(\d{2})(?!\d)/, '$120$2');
dateStr = dateStr.replace(/(\d)(am|pm)/, '$1 $2');
if (dateStr.indexOf('/') == -1) //prepend baseDate if no date part
dateStr = '1/1/1970 ' + dateStr; //e.g. "10:00 am" --> "1/1/1970 10:00 am"
else
{ //MM/dd --> MM/dd/yyyy current-year
/*
dateStr = dateStr.replace(/(\d{1,2}\/\d{1,2})(?!\d)/, '$1/' + new Date().getFullYear());
if (!/\d{1,2}\/\d{1,2}\/\d/.test(dateStr)) //MM/yyyy --> MM/1/yyyy 1st day of month
dateStr = dateStr.replace(/(\d{1,2})\/(\d{4})/, '$1/1/$2');
dateStr = dateStr.replace(/(\d{4})\/(\d{1,2})(?!\/)/, '$2/1/$1'); //yyyy/MM --> MM/1/yyyy
dateStr = dateStr.replace(/(\d{4})\/(\d{1,2}\/\d{1,2})(?!\/)/,'$2/$1'); //yyyy/MM/dd --> MM/dd/yyyy
*/
}
dateStr = dateStr.trim();
date = new Date(dateStr);
if (!isNaN(date)) //if valid date, return it otherwise check for out of order dates
return date; //-OR- filename timestamps -- dashes already eliminated above
dateStr = dateStr.replace(/(\d{1,2}\/\d{1,2}\/\d{4})\./, '$1 '); //timestamps . after number year
dateStr = dateStr.replace(/(\d{1,2})[~._](\d{1,2})(?!\d)/g, '$1:$2'); //##[~. _]## --> ##:##
dateStr = dateStr.replace(/(\d{1,4})\s*@\s(\d{1,2})/g, '$1 $2'); //## @ ## --> ## ##
//e.g. 12/2015 @ 10:30
return new Date(dateStr);
}
//________________________________________________________________________________________
/**
* bind EZ.date.fn with nested instance functions and external EZ.date.* functions
* -AND- EZ.date.prototype.{functionName}
* -AND- EZ.date.prototype.callback.{functionName}
*/
function setProperties(date)
{
//date.toStringDefault = date.toString; //copy native Date.toString()
date.toString = EZ.date.bind(date, '_toString');
date._options.bindNames = Object.keys(EZ.date.fnMap);
for (var name in EZ.date.fnMap) //bind nested instance date functions
date[name] = EZ.date.bind(date, '_' + name);
Object.keys(window.EZ.date).remove('test').forEach(function(name)
{ //bind external EZ.date[fn] as instance
var fn = window.EZ.date[name];
if (typeof(fn) != 'function') return;
date._options.bindNames.push(name);
date[name] = fn.bind(date);
});
Object.keys(EZ.date.prototype).remove('setup').forEach(function(key)
{ //for each EZ.date.prototype . . .
var fn = window.EZ.date.prototype[key];
if (typeof(fn) != 'function') //save property if not function
return date[key] = fn;
var name = fn.name || key; //if name not internal instance fn,
if (!date._options.bindNames.includes[name])
date[name] = fn.bind(date); //bind directly as external instance fn
else if (EZ.options.date.callback) //otherwise if callback enabled
options.callback[name] = fn; //save as callback fn
});
}
}
//____________________________________________________________________________________________
/**
* called by EZ.date() to initialize RZ.date() call options with:
* fn, ctx, args, message, rtnValue, callback . . .
*/
function getDateOptions(date, args)
{
if (typeof(date) == 'object' && date.exception) return date;
var options = {
ctx: date,
args: EZ.toArray(args),
rtnValue: date,
message: '',
callback: {},
};
do
{
var fn = args[0];
if (typeof(fn) != 'string' || fn.substr(0,1) != '_')
{ //createDate call
if (EZ.options.date.formats == null)
dateSetup();
date = new Date(''); //start with invalid date
options.date = date;
fn = options.fn = 'createDate';
options.fnRun = createDate;
options.lock = true;
}
else //other instance function call
{
fn = options.fn = fn.substr(1);
options.args.shift();
var type = getType(date); //must be called from date instance
if (type != 'Date')
{
options.message = fn + '() only valid for EZ.Date'
+ (type != 'Window' ? ' not ' + type : '');
break;
}
if (fn == 'toString')
{
if (date.isEZ) //if EZ.date initialized
options.fnRun = _toString; //use internal toString()
//rtnValue = date.toStringDefault() || date.toStringDefault();
}
else if (!date._options.bindNames.includes(fn))
{
options.message = 'unknown EZ.date function: ' + fn;
break;
}
else
{
options.fnRun = EZ.date.fnMap[fn]; //lookup internal function
/// if (fn in date._options.callback) //check for callback
/// options.callback = date._options.callback[fn];
}
}
//var lockList = 'createDate toString callback'.split(' ');
}
while (false)
//=============
//console.log(options)
return options;
//=============
}
/*--------------------------------------------*/
/*BOOKMARK EZ.date date (instance) functions */
/*------------------------------------------*/
//____________________________________________________________________________________________
/**
* return full month name
*/
function _getMonthName()
{
return EZ.date.getMonthNames()[this.getMonth()];
}
/**
* return full day name
*/
function _getDayName()
{
return EZ.date.getDayNames()[this.getDay()];
}
/**
* return number of days in date's month
*/
function _getDaysInMonth()
{
// 1 2 3 4 5 6 7 8 9 10 11 12
var daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
var month = this.getMonth();
var days = daysInMonth[month];
if (month==1 && isLeapYear(this.getFullYear()))
days=29;
return this.callback(days);
function isLeapYear(Year)
{
if (((Year % 4)===0) && ((Year % 100)!==0) || ((Year % 400)===0))
return true;
else
return false;
}
/* alt from: https://msdn.microsoft.com/en-us/library/aa239571(v=vs.60).aspx
// may not work in non-IE browsers
eomDate = new Date();
eomDate.setMonth(eomDate.getMonth() + 1 );
eomDate.setDate(1);
eomDate.setDate( eomDate.getDate() - 1);
alert("Last day of this month = " + eomDate);
*/
}
/**
* return new EZdate with number of months added or subtracted -- default: +1
*/
function _addYears(number)
{
number = EZ.toInt(number, 1);
return _addMonths(number * 12);
}
/**
* return new EZdate with number of months added or subtracted -- default: +1
*/
function _addMonths(number)
{
number = EZ.toInt(number, 1);
/// var time = this.getTime();
var year = this.getFullYear() + parseInt(number / 12);
var month = (this.getMonth() + number) % 12;
if (number > 0 && month < this.getMonth())
year++;
else if (number < 0 && month > this.getMonth())
year--;
var day = this.getDate();
var hours = this.getSeconds();
var minutes = this.getSeconds();
var seconds = this.getSeconds();
var milliseconds = this.getMilliseconds();
var date = new Date(year,month,day,hours,minutes,seconds,milliseconds);
date.setDate(1); //advance to next month using day=1
date.setYear(year);
date.setMonth(month);
day = Math.min(day, this.getDaysInMonth());
date.setDate(day); //adjust date using current day or last day of month
return createDate(date);
}
/**
* return new EZdate with number of weeks added or subtracted -- default: +1
*/
function _addWeeks(number)
{
if (typeof number == 'undefined') number = 1;
return _addDays(number*7);
}
/**
* TODO: return new EZdate with number of days added or subtracted -- default: +1
*
* Credit: https://www.w3schools.com/js/js_obj_date.asp
* They say adding days goes into another month or year, Date object adjusts.
*/
function _addDays(number)
{
var milliseconds = EZ.toInt(number, 1) * (60 * 60 * 1000) * 24;
return _addMilliseconds(milliseconds);
}
/**
* return new EZdate with number of hours added or subtracted -- default: +1
*/
function _addHours(number)
{
var milliseconds = EZ.toInt(number, 1) * (60 * 60 * 1000);
return _addMilliseconds(milliseconds);
}
/**
* return new EZdate with number of minutes added or subtracted -- default: +1
*/
function _addMinutes(number)
{
var milliseconds = EZ.toInt(number, 1) * (60 * 1000);
return _addMilliseconds(milliseconds);
}
/**
* return new EZdate with number of minutes added or subtracted -- default: +1
*/
function _addSeconds(number)
{
var milliseconds = EZ.toInt(number, 1) * (60 * 1000);
return _addMilliseconds(milliseconds);
}
/**
* return new EZdate with number of minutes added or subtracted -- default: +1
*/
function _addMilliseconds(number)
{
var milliseconds = EZ.toInt(number, 1);
return createDate(this.getTime() + milliseconds);
}
/**
* return new EZ.date() object containing only date part of this EZ.date().
*/
function _getDatePart()
{
var date = EZdate(this.getFullYear(),this.getMonth(),this.getDate())
return this.callback( EZ.date(date) );
}
/**
* return a new Date() object which is GMT representation of time component.
*
* Therefore getTime() returns milliseconds since midnight not since GMT 12:00am
* (e.g. 0 for midnight; 1*60*60*1000 = 3600000 for 1am, etc).
*
* However getHours() returns GMT hours not the hour specified when this EZ.date
* object was created. Therefore EZ.date() with no time component (i.e. midnight)
* returns 19 (7pm) from getHours() when local is EST since EST is GMT-5 hours.
*/
function _getTimePart()
{
var date = new Date(1970,0,1);
date.setUTCHours(this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds());
return this.callback( EZ.date(date) );
}
/**
* Returns the week number for this date.
*
* @param int dowOffset (optional) day week starts on (0 to 6) 1 is Monday
* @return int ISO 8601 week number
*
* Credit: Nick Baicoianu at MeanFreePath: https://www.meanfreepath.com
*/
function _getWeek(dowOffset)
{
dowOffset = isNaN(dowOffset) ? 0 : dowOffset; //default dowOffset to zero
var newYear = new Date(this.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset; //the day of week the year begins on
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((this.getTime() - newYear.getTime() -
(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weeknum;
//if the year starts before the middle of a week
if(day < 4)
{
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52)
{
var nYear = new Date(this.getFullYear() + 1,0,1);
var nday = nYear.getDay() - dowOffset;
var nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/
weeknum = nday < 4 ? 1 : 53;
}
}
else
weeknum = Math.floor((daynum+day-1)/7);
return this.callback(weeknum);
}
//_________________________________________________________________________________________
/**
* called before createDate() when EZ.options.date.formats undefined
* -initialize EZ.options.date.formats to defaultFormats
* -populated EZ.date.fnMap with internal functions
*/
function dateSetup()
{
EZ.date.fnMap = {
addYears: _addYears,
addMonths: _addMonths,
addWeeks: _addWeeks,
addDays: _addDays,
addHours: _addHours,
addMinutes: _addMinutes,
addSeconds: _addSeconds,
addMilliseconds: _addMilliseconds,
getMonthName: _getMonthName,
getDayName: _getDayName,
getDaysInMonth: _getDaysInMonth,
//getNthDayInMonth: _getNthDayInMonth,
getWeek: _getWeek,
//updateFormattedDate: _updateFormattedDate,
getDatePart: _getDatePart,
getTimePart: _getTimePart
//toString: _toString
}
EZ.options.date.formats = EZ.mergeAll(EZ.options.date.defaultFormats);
if (EZ.date.prototype.setup)
EZ.date.prototype.setup();
}
//_________________________________________________________________________________________
/**
* return type as defined by Object.prototype.toString()
* use constructor.name if type is lowercase e.g. window --> [object global]
*/
function getType(value)
{
var type = Object.prototype.toString.call(value);
type = type.substring(8,type.length-1)
if (type.toLowerCase() == type)
type = value.constructor.name;
return type;
}
}
/*---------------------------------------------------------------------------------------------
EZ.date.getFormats(formats)
return Object of valid formats matching those specified or all valid formats if none specified.
ARGUMENTS:
formatName (optional) space delimited string or Array containing one or more format names
RETURNS:
Object with valid format names as key and value as format notation.
Use: date.toString(formatNames...) to get date formatted with specified format name(s).
---------------------------------------------------------------------------------------------*/
EZ.date.getFormats = function EZdate_getFormats(formatNames)
{
if (EZ.options.date.formats == null) EZ.date();
var formatOptions = this.isEZ ? this._formats || {} : EZ.options.date.formats;
var formats = {};
formatNames = formatNames || Object.keys(formatOptions);
EZ.toArray(formatNames, true).forEach(function(key)
{
if (formatOptions[key])
formats[key] = formatOptions[key];
});
return formats;
}
/*---------------------------------------------------------------------------------------------
EZ.date.setFormats(formats)
Adds or replaces date format strings. If called on EZ.date Date Object, only applies to that
date otherwise applies to any new EZ.date created via EZ.date(...)
ARGUMENTS:
formats Object containing key/values: format name as key and date notation as value.
RETURNS:
Object of updated formats as returned by getFormats()
---------------------------------------------------------------------------------------------*/
EZ.date.setFormats = function EZdate_setFormats(formats)
{
if (EZ.options.date.formats == null) EZ.date();
var formatOptions = this.isEZ ? this._formats || {} : EZ.options.date.formats;
formats = formats || EZ.options.date.defaultFormats;
for (var key in formats)
formatOptions[key] = formats[key] || EZ.options.date.defaultFormats[key] || '';
if (this.isEZ)
this.updateFormattedDate();
return this.getFormats();
}
/*---------------------------------------------------------------------------------------------
EZ.date.removeFormats(formats)
ARGUMENTS:
formats space delimited Strimg or Array of formats to remove.
RETURNS:
Object of updated formats as returned by getFormats()
---------------------------------------------------------------------------------------------*/
EZ.date.removeFormats = function EZdate_removeFormats(formatNames)
{
if (EZ.options.date.formats == null) EZ.date();
var formatOptions = this.isEZ ? this._formats || {} : EZ.options.date.formats;
formatNames = formatNames || Object.keys(EZ.date.getFormats());
EZ.toArray(formatNames, true).forEach(function(key)
{
delete formatOptions[key];
});
if (this.isEZ)