-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (71 loc) · 2.16 KB
/
index.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
class DateTime {
constructor() {
this.days = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
this.months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
}
getCurrentDay = () => {
const dayIndex = new Date().getDay();
return this.days[dayIndex];
};
getCurrentMonth = () => {
const monthIndex = new Date().getMonth();
return this.months[monthIndex];
};
getFormattedDateTime = () => {
const now = new Date();
const year = now.getFullYear();
const month = this.formatNumber(now.getMonth() + 1);
const day = this.formatNumber(now.getDate());
const hours = this.formatNumber(now.getHours());
const minutes = this.formatNumber(now.getMinutes());
const seconds = this.formatNumber(now.getSeconds());
return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
};
formatNumber = (num) => {
return num < 10 ? '0' + num : num;
};
getYesterday = () => {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
return this.formatDate(yesterday);
};
getTomorrow = () => {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
return this.formatDate(tomorrow);
};
formatDate = (date) => {
const year = date.getFullYear();
const month = this.formatNumber(date.getMonth() + 1);
const day = this.formatNumber(date.getDate());
return `${year}/${month}/${day}`;
};
formatTime = (date) => {
const hours = this.formatNumber(date.getHours());
const minutes = this.formatNumber(date.getMinutes());
const seconds = this.formatNumber(date.getSeconds());
return `${hours}:${minutes}:${seconds}`;
};
}
export default DateTime;