Fork me on GitHub

Date的那些事

日期格式化

1
2
3
4
5
6
7
8
9
10
11
12
13

function dateToString(date) {
if (date && date instanceof Date) {
var yyyy = date.getFullYear();
var mm = date.getMonth() < 9 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
var dd = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var min = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
return "".concat(yyyy).concat('-').concat(mm).concat('-').concat(dd).concat('-').concat(ss);
}
return '';
}

模拟moment插件

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

/**
* 格式化时间
*/
myMoment = (date = new Date().getTime()) => {
this.date = new Date(date)
return this;
}

/**
* 输入格式化格式
*/
formate = (formatStr = 'YYYY-MM-DD HH:mm:ss') => {
const date = this.date
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const week = date.getDay()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()

return formatStr.replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}/g, (match) => {
switch (match) {
case 'YY':
return String(year).slice(-2)
case 'YYY':
case 'YYYY':
return String(year)
case 'M':
return String(month)
case 'MM':
return String(month).padStart(2, '0')
case 'D':
return String(day)
case 'DD':
return String(day).padStart(2, '0')
case 'd':
return String(week)
case 'dd':
return weeks[week]
case 'ddd':
return '周' + weeks[week]
case 'dddd':
return '星期' + weeks[week]
case 'H':
return String(hour)
case 'HH':
return String(hour).padStart(2, '0')
case 'm':
return String(minute)
case 'mm':
return String(minute).padStart(2, '0')
case 's':
return String(second)
case 'ss':
return String(second).padStart(2, '0')
default:
return match
}
})
}

获取当前日期的周边

今天

1
2
3
4
5
function showToDay() {
var Nowdate = new Date();
M = Number(Nowdate.getMonth()) + 1
return Nowdate.getFullYear() + "-" + M + "-" + Nowdate.getDate();
}

明天

1
2
3
4
5
6
7

function showTomorrow() {
var tom = new Date();
tom.setDate(tom.getDate() + 1);
M = Number(tom.getMonth()) + 1
return tom.getFullYear() + "-" + M + "-" + tom.getDate();
}

本周第一天

1
2
3
4
5
6
function showWeekFirstDay() {
var Nowdate = new Date();
var WeekFirstDay = new Date(Nowdate - (Nowdate.getDay() - 1) * 86400000);
M = Number(WeekFirstDay.getMonth()) + 1
return WeekFirstDay.getFullYear() + "-" + M + "-" + WeekFirstDay.getDate();
}

本周最后一天

1
2
3
4
5
6
7
function showWeekLastDay() {
var Nowdate = new Date();
var WeekFirstDay = new Date(Nowdate - (Nowdate.getDay() - 1) * 86400000);
var WeekLastDay = new Date((WeekFirstDay / 1000 + 6 * 86400) * 1000);
M = Number(WeekLastDay.getMonth()) + 1
return Nowdate.getFullYear() + "-" + M + "-" + WeekLastDay.getDate();
}

本月第一天

1
2
3
4
5
6
function showMonthFirstDay() {
var Nowdate = new Date();
var MonthFirstDay = new Date(Nowdate.getFullYear(), Nowdate.getMonth(), 1);
M = Number(MonthFirstDay.getMonth()) + 1
return MonthFirstDay.getFullYear() + "-" + M + "-" + MonthFirstDay.getDate();
}

本月最后一天

1
2
3
4
5
6
7
function showMonthLastDay() {
var Nowdate = new Date();
var MonthNextFirstDay = new Date(Nowdate.getFullYear(), Nowdate.getMonth() + 1, 1);
var MonthLastDay = new Date(MonthNextFirstDay - 86400000);
M = Number(MonthLastDay.getMonth()) + 1
return MonthLastDay.getFullYear() + "-" + M + "-" + MonthLastDay.getDate();
}