-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateTime.mjs
132 lines (108 loc) · 3.71 KB
/
dateTime.mjs
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
"use strict"
import { UNDEFINED } from "./coreJs.mjs";
const fullDaysFactor = 86400000;
const date = new Date(0);
export const dateToDateTime = (input) => input * fullDaysFactor;
export const dateTimeToDate = (input) => input / fullDaysFactor;
export const Year = 0;
export const Month = 1;
export const Day = 2;
export const Hour = 3;
export const Minute = 4;
export const Second = 5;
export const Milliecond = 6;
export const dateTimeToString = (input, also = Year) => {
if (isNaN(input))
return UNDEFINED;
date.setTime(input);
const year = date.getUTCFullYear().toString().padStart(4, '0');
if (also === Year)
return year;
const month = (date.getUTCMonth() + 1).toString().padStart(2, '0');
if (also === Month)
return `${year}-${month}`;
const day = date.getUTCDate().toString().padStart(2, '0');
return `${year}-${month}-${day}`;
};
export const dateToString = (input, also) => {
if (isNaN(input))
return UNDEFINED;
return dateTimeToString(dateToDateTime(input), also);
};
export const stringToDateTime = (input, expect = Year) => {
if (!input)
return UNDEFINED;
const splitted = input.split('-').filter(s => s.trim());
if ((expect === Day) && (splitted.length < 3))
return UNDEFINED;
else if ((expect === Month) && (splitted.length < 2))
return UNDEFINED;
const asNumbers = splitted.map(s => parseInt(s));
if (splitted[0].length < 4) {
if (asNumbers[0] < 70) {
asNumbers[0] += 2000;
} else if (asNumbers[0] < 100) {
asNumbers[0] += 1900;
}
}
while (asNumbers.length < 3)
asNumbers.push(1);
if (asNumbers[1] < 1 || asNumbers[1] > 12)
return UNDEFINED;
if (asNumbers[2] < 1 || asNumbers[2] > 31)
return UNDEFINED;
asNumbers[1]--;
date.setTime(0);
date.setUTCFullYear(asNumbers[0]);
date.setUTCMonth(asNumbers[1]);
date.setUTCDate(asNumbers[2]);
return date.getTime();
};
export const stringToDate = (input, expect) => {
if (!input)
return UNDEFINED;
const value = stringToDateTime(input, expect);
if (value === UNDEFINED)
return UNDEFINED;
return dateTimeToDate(value);
};
export const timeToString = (input, also = Hour, separator = '.') => {
if (isNaN(input))
return UNDEFINED;
const date = new Date(input);
const hours = date.getUTCHours().toString().padStart(2, '0');
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
let result = `${hours}:${minutes}`;
if (also === Minute)
return result;
const seconds = date.getUTCSeconds().toString().padStart(2, '0');
result += `:${seconds}`;
if (also === Second)
return result;
const milliseconds = date.getUTCMilliseconds().toString().padStart(3, '0');
return `${result}${separator}${milliseconds}`;
};
export const stringToTime = (input, expect = Hour) => {
if (!input)
return UNDEFINED;
const splitted = input.split(':').filter(s => s.trim());
if (splitted.length < 2)
return UNDEFINED;
if ((expect === Second) && (splitted.length < 3))
return UNDEFINED;
const asNumbers = splitted.map(s => parseInt(s));
while (asNumbers.length < 3)
asNumbers.push(0);
if (asNumbers[0] < 0 || asNumbers[0] > 23)
return UNDEFINED;
if (asNumbers[1] < 0 || asNumbers[1] > 59)
return UNDEFINED;
if (asNumbers[2] < 0 || asNumbers[2] >= 60)
return UNDEFINED;
date.setTime(0);
date.setUTCHours(asNumbers[0]);
date.setUTCMinutes(asNumbers[1]);
date.setUTCSeconds(asNumbers[2]);
return date.getTime();
};
export const getTimestamp = () => Date.now();