-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdate.ts
41 lines (38 loc) · 1.11 KB
/
date.ts
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
// const format = n => n > 9 ? n : `0${n}`;
const format = (n: number) => n.toString().padStart(2, "0");
/**
* Dater extends Date to provide more formating methods.
*/
export default class Dater extends Date {
/**
* Get date string as yyyy-mm-dd hh:mm:ss
* @param separator
* @returns
*/
override toLocaleString(separator = "-"): string {
const dateString = this.toLocaleDateString(separator);
const timeString = this.toLocaleTimeString();
return `${dateString} ${timeString}`;
}
/**
* Get date string as yyyy-mm-dd
* @param separator
* @returns
*/
override toLocaleDateString(separator = "-"): string {
const year = format(this.getFullYear());
const month = format(this.getMonth() + 1);
const day = format(this.getDate());
return `${year}${separator}${month}${separator}${day}`;
}
/**
* Get time string as hh:mm:ss
* @returns
*/
override toLocaleTimeString(): string {
const hour = format(this.getHours());
const minute = format(this.getMinutes());
const second = format(this.getSeconds());
return `${hour}:${minute}:${second}`;
}
}