-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (109 loc) · 4.31 KB
/
Copy pathindex.js
File metadata and controls
139 lines (109 loc) · 4.31 KB
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
'use strict';
(function ($global) {
const __dateTime = {};
__dateTime.getUnixTimeSec = ($dateTime, $ms = false) => {
if ('object' === typeof $dateTime && $dateTime instanceof Date) {
return Math.round($dateTime.getTime() / 1000);
}
if ('number' === typeof $dateTime) {
return $ms ? Math.round($dateTime / 1000) : $dateTime;
}
if ('string' === typeof $dateTime) {
if (!isNaN(Number($dateTime))) {
return $ms ? Math.round(Number($dateTime) / 1000) : Number($dateTime);
} else {
const __tmpVar = Math.round(new Date($dateTime).getTime() / 1000);
return isNaN(__tmpVar) ? 'Invalid Date' : __tmpVar;
}
}
return 'Invalid Date';
};
__dateTime.getDateZeroTime = ($dateTime, $ms = false) => {
let __thisDateTime;
if ('object' === typeof $dateTime && $dateTime instanceof Date) {
__thisDateTime = $dateTime
}
if ('number' === typeof $dateTime) {
__thisDateTime = $ms ? new Date($dateTime) : new Date($dateTime * 1000);
}
if ('string' === typeof $dateTime) {
if (!isNaN(Number($dateTime))) {
__thisDateTime = $ms ? new Date(Number($dateTime)) : new Date(Number($dateTime) * 1000);
} else {
__thisDateTime = new Date($dateTime);
}
}
if ('Invalid Date' === $dateTime.toString()) {
return 'Invalid Date';
}
__thisDateTime.setHours(0);
__thisDateTime.setMinutes(0);
__thisDateTime.setSeconds(0);
__thisDateTime.setMilliseconds(0);
return __thisDateTime;
};
__dateTime.dateAdd = ($date, $days) => {
$date.setDate($date.getDate() + $days);
return $date;
};
__dateTime.dateDiff = ($start_date, $end_date) => {
const __one_day = 86400, __one_hour = 3600, __one_min = 60;
const __start_date = __dateTime.getUnixTimeSec($start_date);
if ('Invalid Date' === __start_date) {
return 'Invalid Start Date';
}
const __end_date = __dateTime.getUnixTimeSec($end_date);
if ('Invalid Date' === __end_date) {
return 'Invalid End Date';
}
let __time_value = __start_date - __end_date;
let {pof, val, days, day, hour, min, sec} = {pof: true, val: 0, days: 0, day: 0, hour: 0, min: 0, sec: 0};
pof = __time_value < 0;
val = Math.abs(__time_value);
const __days = val / __one_day,
__hour = val % __one_day / __one_hour,
__min = val % __one_day % __one_hour / __one_min;
days = __days.toFixed(2);
day = __days >= 1 ? parseInt(__days) : 0;
hour = __hour >= 1 ? parseInt(__hour) : 0;
min = __min >= 1 ? parseInt(__min) : 0;
sec = val % __one_day % __one_hour % __one_min;
return {pof, val, days, day, hour, min, sec};
};
__dateTime.format = ($date, $pattern = 'YYYY-MM-DD HH:mm:ss') => {
const functionCall = {
YYYY: ['getFullYear', 4],
YY: ['getFullYear', 2],
MM: ['getMonth', 2, 1],
DD: ['getDate', 2],
HH: ['getHours', 2],
mm: ['getMinutes', 2],
ss: ['getSeconds', 2],
ms: ['getMilliseconds', 3]
};
function dateFormat($date, $pattern) {
function findMatch() {
const matching = /(?=(YYYY|YY|MM|DD|HH|mm|ss|ms))\1([:\/]*)/.exec($pattern);
if (matching) {
const hitTarget = functionCall[matching[1]];
const addPrefix = '00' + String($date[hitTarget[0]]() + (hitTarget[2] || 0));
const result = addPrefix.slice(-hitTarget[1]) + (matching[2] || '');
$pattern = $pattern.replace(matching[0], result);
findMatch();
}
}
findMatch();
return $pattern
}
return dateFormat($date, $pattern)
};
if ('function' === typeof define && define.amd) {
define(function () {
return __dateTime;
});
} else if ('object' === typeof exports) {
module.exports = __dateTime;
} else {
$global.__DateTime = __dateTime;
}
})(this);