-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathdate.ts
More file actions
188 lines (175 loc) · 4.95 KB
/
date.ts
File metadata and controls
188 lines (175 loc) · 4.95 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
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
180
181
182
183
184
185
186
187
188
// only set once.
let TIMEZONE = '';
export function resetTimezone() {
TIMEZONE = '';
let _hourOffset = Math.floor(-(new Date().getTimezoneOffset()) / 60);
if (_hourOffset >= 0) {
TIMEZONE += '+';
} else {
TIMEZONE += '-';
}
_hourOffset = Math.abs(_hourOffset);
const _hourOffsetStr = _hourOffset < 10 ? `0${_hourOffset}` : `${_hourOffset}`;
TIMEZONE += `${_hourOffsetStr}00`;
return TIMEZONE;
}
resetTimezone();
const MONTHS: Record<string, string> = {
'01': 'Jan',
'02': 'Feb',
'03': 'Mar',
'04': 'Apr',
'05': 'May',
'06': 'Jun',
'07': 'Jul',
'08': 'Aug',
'09': 'Sep',
// eslint-disable-next-line quote-props
'10': 'Oct',
// eslint-disable-next-line quote-props
'11': 'Nov',
// eslint-disable-next-line quote-props
'12': 'Dec',
};
/**
* return `[ YYYY, MM, DD, HH, mm, ss ]` date string array
*/
export function getDateStringParts(d?: Date, onlyDate?: boolean) {
d = d || new Date();
const monthNum = d.getMonth() + 1;
const month = monthNum < 10 ? `0${monthNum}` : `${monthNum}`;
const dateNum = d.getDate();
const date = dateNum < 10 ? `0${dateNum}` : `${dateNum}`;
if (onlyDate) {
return [ `${d.getFullYear()}`, month, date ];
}
const hoursNum = d.getHours();
const hours = hoursNum < 10 ? `0${hoursNum}` : `${hoursNum}`;
const minutesNum = d.getMinutes();
const minutes = minutesNum < 10 ? `0${minutesNum}` : `${minutesNum}`;
const secondsNum = d.getSeconds();
const seconds = secondsNum < 10 ? `0${secondsNum}` : `${secondsNum}`;
return [ `${d.getFullYear()}`, month, date, hours, minutes, seconds ];
}
/**
* Access log format date. format: `moment().format('DD/MMM/YYYY:HH:mm:ss ZZ')`
*/
export function accessLogDate(d?: Date): string {
// 16/Apr/2013:16:40:09 +0800
d = d || new Date();
const [ year, month, date, hours, minutes, seconds ] = getDateStringParts(d);
return `${date}/${MONTHS[month]}/${year}:${hours}:${minutes}:${seconds} ${TIMEZONE}`;
}
/**
* Normal log format date. format: `moment().format('YYYY-MM-DD HH:mm:ss.SSS')`
*/
export function logDate(msSep?: string): string;
export function logDate(d?: Date): string;
export function logDate(d?: Date | null, msSep?: string): string;
export function logDate(d?: Date | string | null, msSep?: string): string {
if (typeof d === 'string') {
// logDate(msSep)
msSep = d;
d = new Date();
} else {
// logDate(d, msSep)
d = d || new Date();
}
const [ year, month, date, hours, minutes, seconds ] = getDateStringParts(d);
const millisecondsNum = d.getMilliseconds();
let milliseconds = `${millisecondsNum}`;
if (millisecondsNum < 10) {
milliseconds = `00${millisecondsNum}`;
} else if (millisecondsNum < 100) {
milliseconds = `0${millisecondsNum}`;
}
msSep = msSep || '.';
return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}${msSep}${milliseconds}`;
}
export const YYYYMMDDHHmmssSSS = logDate;
export interface YYYYMMDDHHmmssOptions {
dateSep?: string;
timeSep?: string;
}
/**
* `moment().format('YYYY-MM-DD HH:mm:ss')` format date string.
*/
export function YYYYMMDDHHmmss(d?: Date | string | number, options?: YYYYMMDDHHmmssOptions): string {
d = d || new Date();
if (!(d instanceof Date)) {
d = new Date(d);
}
let dateSep = '-';
let timeSep = ':';
if (options?.dateSep) {
dateSep = options.dateSep;
}
if (options?.timeSep) {
timeSep = options.timeSep;
}
const [ year, month, date, hours, minutes, seconds ] = getDateStringParts(d);
return `${year}${dateSep}${month}${dateSep}${date} ${hours}${timeSep}${minutes}${timeSep}${seconds}`;
}
/**
* `moment().format('YYYY-MM-DD')` format date string.
*/
export function YYYYMMDD(d?: Date | string, sep?: string): string {
if (typeof d === 'string') {
// YYYYMMDD(sep)
sep = d;
d = new Date();
} else {
// YYYYMMDD(d, sep)
d = d || new Date();
if (typeof sep !== 'string') {
sep = '-';
}
}
const [ year, month, date ] = getDateStringParts(d, true);
return `${year}${sep}${month}${sep}${date}`;
}
export interface DateStruct {
YYYYMMDD: number;
H: number;
}
/**
* return datetime struct.
*
* @return {Object} date
* - {Number} YYYYMMDD, 20130401
* - {Number} H, 0, 1, 9, 12, 23
*/
export function datestruct(now?: Date): DateStruct {
now = now || new Date();
return {
YYYYMMDD: now.getFullYear() * 10000 + (now.getMonth() + 1) * 100 + now.getDate(),
H: now.getHours(),
} satisfies DateStruct;
}
/**
* Get Unix's timestamp in seconds.
*/
export function timestamp(t?: number | string): number | Date {
if (t) {
// convert timestamp to Date
// timestamp(timestampValue)
let v: number;
if (typeof t === 'string') {
v = Number(t);
} else {
v = t;
}
if (String(v).length === 10) {
v *= 1000;
}
return new Date(v);
}
// get current timestamp
return Math.round(Date.now() / 1000);
}
/**
* Parse timestamp to Date
*/
export function parseTimestamp(t: number | string): Date {
return timestamp(t) as Date;
}