-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsDate.js
93 lines (70 loc) · 3.19 KB
/
jsDate.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
/*
Name: jsDate
Desc: VBScript native Date functions emulated for Javascript
Author: Rob Eberhardt, Slingshot Solutions - http://slingfive.com/
Note: see jsDate.txt for more info
*/
/*
I (Gabe) modified it to fit my needs better
1. Changed DateDiff to return an object of all calculated offsets
2. Removed any functions I wasn't using
*/
// constants
vbGeneralDate=0; vbLongDate=1; vbShortDate=2; vbLongTime=3; vbShortTime=4; // NamedFormat
vbUseSystemDayOfWeek=0; vbSunday=1; vbMonday=2; vbTuesday=3; vbWednesday=4; vbThursday=5; vbFriday=6; vbSaturday=7; // FirstDayOfWeek
vbUseSystem=0; vbFirstJan1=1; vbFirstFourDays=2; vbFirstFullWeek=3; // FirstWeekOfYear
Date.IsDate = function(p_Expression){
return !isNaN(new Date(p_Expression)); // <-- review further
}
Date.CDate = function(p_Date){
if(Date.IsDate(p_Date)){ return new Date(p_Date); }
var strTry = p_Date.replace(/\-/g, '/').replace(/\./g, '/').replace(/ /g, '/'); // fix separators
strTry = strTry.replace(/pm$/i, " pm").replace(/am$/i, " am"); // and meridian spacing
if(Date.IsDate(strTry)){ return new Date(strTry); }
var strTryYear = strTry + '/' + new Date().getFullYear(); // append year
if(Date.IsDate(strTryYear)){ return new Date(strTryYear); }
if(strTry.indexOf(":")){ // if appears to have time
var strTryYear2 = strTry.replace(/ /, '/' + new Date().getFullYear() + ' '); // insert year
if(Date.IsDate(strTryYear2)){ return new Date(strTryYear2); }
var strTryDate = new Date().toDateString() + ' ' + p_Date; // pre-pend current date
if(Date.IsDate(strTryDate)){ return new Date(strTryDate); }
}
return false; // double as looser IsDate
//throw("Error #13 - Type mismatch"); // or is this better?
}
Date.DateDiff = function(p_Date1, p_Date2, p_FirstDayOfWeek){
p_Interval = "ms";
if(!Date.CDate(p_Date1)){ return "invalid date: '" + p_Date1 + "'"; }
if(!Date.CDate(p_Date2)){ return "invalid date: '" + p_Date2 + "'"; }
p_FirstDayOfWeek = (isNaN(p_FirstDayOfWeek) || p_FirstDayOfWeek==0) ? vbSunday : parseInt(p_FirstDayOfWeek); // set default & cast
var dt1 = Date.CDate(p_Date1);
var dt2 = Date.CDate(p_Date2);
// correct DST-affected intervals ("d" & bigger)
if(p_Date1.toString().indexOf(":") ==-1){ dt1.setUTCHours(0,0,0,0) }; // no time, assume 12am
if(p_Date2.toString().indexOf(":") ==-1){ dt2.setUTCHours(0,0,0,0) }; // no time, assume 12am
// get ms between UTC dates and make into "difference" date
var iDiffMS = dt2.valueOf() - dt1.valueOf();
var dtDiff = new Date(iDiffMS);
// calc various diffs
var nYears = dt2.getUTCFullYear() - dt1.getUTCFullYear();
var nMonths = dt2.getUTCMonth() - dt1.getUTCMonth() + (nYears!=0 ? nYears*12 : 0);
var nQuarters = parseInt(nMonths / 3); //<<-- different than VBScript, which watches rollover not completion
var nMilliseconds = iDiffMS;
var nSeconds = parseInt(iDiffMS / 1000);
var nMinutes = parseInt(nSeconds / 60);
var nHours = parseInt(nMinutes / 60);
var nDays = parseInt(nHours / 24); // <-- now fixed for DST switch days
var nWeeks = parseInt(nDays / 7);
// return difference
return {
"yyyy": nYears,
"q": nQuarters,
"m": nMonths,
"d": nDays,
"w": nWeeks,
"h": nHours,
"n": nMinutes,
"s": nSeconds,
"ms": nMilliseconds
};
}