-
Notifications
You must be signed in to change notification settings - Fork 22
/
orb-date-handler.js
128 lines (122 loc) · 3.42 KB
/
orb-date-handler.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
Orb = Orb || {};
// orb-date-handler.js
Orb.DigitsToDate = Orb.DigitsToDate || function (digits) {
var year = Number(digits.substring(0, 4));
var month = Number(digits.substring(4, 6));
var day = Number(digits.substring(6, 8));
if (digits.length > 8) {
var hour = Number(digits.substring(8, 10));
} else {
var hour = 0;
}
if (digits.length > 10) {
var min = Number(digits.substring(10, 12));
} else {
var min = 0;
}
if (digits.length > 12) {
var sec = Number(digits.substring(12, 14));
} else {
var sec = 0;
}
var date = new Date();
date.setTime(Date.UTC(year, month - 1, day, hour, min, sec))
return date;
}
Orb.DateToDigits = Orb.DateToDigits || function (date) {
var year = String(date.getUTCFullYear());
var month = String(date.getUTCMonth() + 1);
if (month < 10) { month = "0" + month }
var day = String(date.getUTCDate());
if (day < 10) { day = "0" + day }
var hour = String(date.getUTCHours());
if (hour < 10) { hour = "0" + hour }
var min = String(date.getUTCMinutes())
if (min < 10) { min = "0" + min }
var sec = String(date.getUTCSeconds());
if (sec < 10) { sec = "0" + sec }
var digits = year + month + day + hour + min + sec;
return digits;
}
Orb.StringToDate = Orb.StringToDate || function (str) {
var str = str.split('Z')[0];
str.match(/(T|_| )/i);
var dt = str.split(RegExp.$1);
var dt0 = dt[0]
if (dt0.match(/\./i)) {
var d = dt0.split(".");
} else {
var d = dt0.split("-");
}
if (d.length > 3) {
d.shift()
d[0] = 0 - Number(d[0]);
}
if (dt[1]) {
var t = dt[1].split(":");
} else {
var t = [];
}
var year = d[0];
var month = d[1];
var day = d[2];
if (t[0]) {
var hours = t[0]
} else {
var hours = 0
};
if (t[1]) {
var minutes = t[1]
} else {
var minutes = 0
};
if (t[2]) {
var sec = t[2].split(".");
var seconds = sec[0];
if (sec[1]) {
var milliseconds = Number("0." + sec[1]) * 1000
} else if (t[3]) {
var milliseconds = t[3];
} else {
var milliseconds = 0;
}
} else {
var seconds = 0;
var milliseconds = 0;
};
var date = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds, milliseconds))
date.setUTCFullYear(year);
return date;
}
Orb.FormatUTCDate = Orb.FormatUTCDate || function (date) {
var year = date.getUTCFullYear()
var month = date.getUTCMonth() + 1
var day = date.getUTCDate()
var hours = date.getUTCHours()
var minutes = date.getUTCMinutes()
var seconds = date.getUTCSeconds()
var milliseconds = date.getUTCMilliseconds()
if (milliseconds > 0) {
seconds = seconds + milliseconds / 1000
}
if (seconds.length < 2) {
seconds = Orb.ZeroFill(seconds, 2)
}
return year + "-" + Orb.ZeroFill(month, 2) + "-" + Orb.ZeroFill(day, 2) + " " + Orb.ZeroFill(hours, 2) + ":" + Orb.ZeroFill(minutes, 2) + ":" + seconds
}
Orb.FormatLocalDate = Orb.FormatLocalDate || function (date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hours = date.getHours()
var minutes = date.getMinutes()
var seconds = date.getSeconds()
var milliseconds = date.getMilliseconds()
if (milliseconds > 0) {
seconds = seconds + milliseconds / 1000
}
if (seconds.length < 2) {
seconds = Orb.ZeroFill(seconds, 2)
}
return year + "-" + Orb.ZeroFill(month, 2) + "-" + Orb.ZeroFill(day, 2) + " " + Orb.ZeroFill(hours, 2) + ":" + Orb.ZeroFill(minutes, 2) + ":" + seconds
}