-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdateTimeUtils.js
180 lines (159 loc) · 5.85 KB
/
dateTimeUtils.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
/* Date related utility functions */
//get Weekdays as an array
function getWeekDays() {
return ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
}
//Get week number
Date.prototype.getWeek = function() {
var dayOne = new Date(this.getFullYear(), 0, 1);
return Math.ceil((((this - dayOne) / 86400000) + dayOne.getDay() + 1) / 7);
//return $.datepicker.iso8601Week(this); //alternative way by using datepicker
}
//Get next working day (not including Saturday and Sunday)
function getNextWorkingDay(date) {
var currentDate = date;
var canRepeat = true;
while (canRepeat) {
var nextDay = new Date(currentDate.setDate(currentDate.getDate() + 1));
if (nextDay.getDay() == 6 || nextDay.getDay() == 0) {
currentDate.setDate(currentDate.getDate() + 1);
canRepeat = true;
} else {
return nextDay;
}
}
}
//Get business days count (do not add Saturday and Suday)
function getBusinessDatesCount(startDate, endDate) {
var count = 0;
var currentDate = startDate
while (currentDate.getTime() <= endDate.getTime()) {
var dayOfWeek = currentDate.getDay();
if(!((dayOfWeek == 6) || (dayOfWeek == 0))) {
count++;
}
currentDate.setDate(currentDate.getDate() + 1);
}
return count;
}
// Convert UTC timestamp to preferred date format.
function UTCToDate(utcTime, dateFormat) {
var date = new Date(utcTime);
var year = date.getFullYear();
var month = ('0' + (date.getMonth() + 1)).slice(-2);
var day = date.getDate();
var formattedDate;
switch(dateFormat) {
case 'MM-DD-YYYY':
formattedDate = month + '-' + day + '-' + year;
break;
case 'DD-MM-YYYY':
formattedDate = day + '-' + month + '-' + year;
break;
case 'YYYY-MM-DD':
formattedDate = year + '-' + month + '-' + day;
break;
case 'MM/DD/YYYY':
formattedDate = month + '/' + day + '/' + year;
break;
case 'DD/MM/YYYY':
formattedDate = day + '/' + month + '/' + year;
break;
case 'YYYY/MM/DD':
formattedDate = year + '/' + month + '/' + day;
break;
default:
formattedDate = year + '-' + month + '-' + day;
}
return formattedDate;
}
function minutesToTimeFormat(minutes) {
if (minutes) {
var mm = parseInt(minutes % 60);
var hh = parseInt((minutes-mm)/60);
return hh.toString() + ':' + (mm <10 ? '0' : '') + mm.toString();
} else {
return '00:00';
}
}
function timeFormatToMinutes(time) {
if (time) {
var timeArray = time.split(':');
hoursValue = parseInt(timeArray[0] * 60) + parseInt(timeArray[1]);
return hoursValue;
}
return 0;
}
function getWeekDaysByWeekNum(weekNum, year) {
if (weekNum && year) {
var today = new Date(year, 0, 1 + ((weekNum - 1) * 7));
var currentWeekDates = [];
var mondayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 1, 0, 0, 0).getTime();
var tuesdayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 2, 0, 0, 0).getTime();
var wednesayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 3, 0, 0, 0).getTime();
var thursdayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 4, 0, 0, 0).getTime();
var fridayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 5, 0, 0, 0).getTime();
var saturdayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 6, 0, 0, 0).getTime();
var sundayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 7, 0, 0, 0).getTime();
currentWeekDates.push(mondayOfWeek);
currentWeekDates.push(tuesdayOfWeek);
currentWeekDates.push(wednesayOfWeek);
currentWeekDates.push(thursdayOfWeek);
currentWeekDates.push(fridayOfWeek);
currentWeekDates.push(saturdayOfWeek);
currentWeekDates.push(sundayOfWeek);
return currentWeekDates;
}
}
//get the time difference between two timestamps as -- -- ago
function timeDifference(current, previous) {
var msPerMinute = 60 * 1000;
var msPerHour = msPerMinute * 60;
var msPerDay = msPerHour * 24;
var msPerMonth = msPerDay * 30;
var msPerYear = msPerDay * 365;
var elapsed = current - previous;
if (elapsed < msPerMinute) {
return Math.round(elapsed/1000) + ' seconds ago';
} else if (elapsed < msPerHour) {
return Math.round(elapsed/msPerMinute) + ' minutes ago';
} else if (elapsed < msPerDay ) {
return Math.round(elapsed/msPerHour ) + ' hours ago';
} else if (elapsed < msPerMonth) {
return Math.round(elapsed/msPerDay) + ' days ago';
} else if (elapsed < msPerYear) {
return Math.round(elapsed/msPerMonth) + ' months ago';
} else {
return Math.round(elapsed/msPerYear ) + ' years ago';
}
}
//is given string a valid date
function isDate(txtDate) {
var currVal = txtDate;
if(currVal == '')
return false;
var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Declare Regex
var dtArray = rxDatePattern.test(txtDate); // is format OK?
if (! dtArray)
return false;
//Checks for mm/dd/yyyy format.
dtMonth = dtArray[1];
dtDay= dtArray[3];
dtYear = dtArray[5];
if (dtMonth < 1 || dtMonth > 12)
return false;
else if (dtDay < 1 || dtDay> 31)
return false;
else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
return false;
else if (dtMonth == 2)
{
var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
if (dtDay> 29 || (dtDay ==29 && !isleap))
return false;
}
return true;
}
function addMinutes(timestamp, minutes) {
return new Date(timestamp + minutes*60000).getTime();
}