1. Introduction
import moment from 'moment'
Two, use
console.log(moment().format("YYYY-MM-DD HH:mm:ss")); //当前时间 (24小时制)
console.log(moment().add(1, "hours").format("YYYY-MM-DD HH:mm:ss")); //当前时间增加1小时
console.log(moment().add(1, "months").format("YYYY-MM-DD HH:mm:ss")); //当前时间增加1个月
console.log(moment().subtract(10, "days").format("YYYY-MM-DD HH:mm:ss")); //当前时间的前10天时间
console.log(moment().subtract(1, "years").format("YYYY-MM-DD HH:mm:ss")); //当前时间的前1年时间
console.log(moment().subtract(3, "months").format("YYYY-MM-DD HH:mm:ss")); //当前时间的前3个月时间
console.log(moment().subtract(1, "weeks").format("YYYY-MM-DD HH:mm:ss")); //当前时间的前一个星期时间
3. The time difference between two dates/times:
moment(endTime).diff(moment(startTime), 'years')
moment(endTime).diff(moment(startTime), 'months')
moment(endTime).diff(moment(startTime), 'days')
// 开始时间和结束时间的时间差,以“天”为单位;endTime和startTime都是毫秒数
moment(endTime).diff(moment(startTime),'minutes' )
moment(endTime).diff(moment(startTime), 'seconds')
console.log(moment(endDate).diff(moment(startDate), 'days'))//查看时间差
Fourth, generate a time list
_getTimeList () {//生成时间列表
const temp = moment(this.searchParams.endDate).diff(moment(this.searchParams.startDate), 'days')
console.log('时间相差天数:', temp)//查看时间间隔
if (temp == 0) {//相差0天,生成24小时列表
const t = moment('00', 'HH')//生成00
this.TIMEList.push(t.format('HH'))//压进去
for (let i = 0; i < 23; i++) {//每次增加一小时
this.TIMEList.push(t.add(1, 'hours').format('HH'))
}
} else {
const t = moment(this.searchParams.startDate)//生成开始日期
this.TIMEList.push(t.format('YYYY-MM-DD'))
for (let i = 0; i < temp; i++) {//每次增加一天
this.TIMEList.push(t.add(1, 'days').format('YYYY-MM-DD'))
}
}
// console.log('TIMEList', this.TIMEList)//查看时间列表
},
5. Get the month and week enumeration list
moment.months()
# ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
moment.monthsShort()
# ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
moment.weekdays()
# ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
moment.weekdaysMin()
# ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
6. Acquisition time
# 获取今天0时0分0秒
moment().startOf('day')
# 获取本周第一天(周日)0时0分0秒
moment().startOf('week')
# 获取本周周一0时0分0秒
moment().startOf('isoWeek')
# 获取当前月第一天0时0分0秒
moment().startOf('month')
# 获取指定日期的0时0分0秒
moment('2019-10-20').startOf('day')
# 获取今天23时59分59秒
moment().endOf('day')
# 获取本周最后一天(周六)23时59分59秒
moment().endOf('week')
# 获取本周周日23时59分59秒
moment().endOf('isoWeek')
# 获取当前月最后一天23时59分59秒
moment().endOf('month')
Normalize returns a time
changeDate (time) {
const d = (new Date(time))
const datetime = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
return datetime
},
Seven, some common sense
format code | illustrate |
---|---|
M | The month represented by a number, without leading 0 |
MM | The month represented by the number, with a leading 0 |
MMM | three letter abbreviation for the month |
MMMM | month, in full text format |
Q | the quarter |
D | Day of the month without leading 0 |
DD | Day of the month with leading 0 |
d | The day of Sunday, expressed as a number |
ddd | Three letters indicating the day of the week |
dddd | day of the week, complete week text |
w | week of the year |
YYYY | Year in full four digits |
YY | two-digit year |
A | Capitalized AM PM |
a | lowercase am pm |
HH | Hour, in 24-hour format, with leading zeros |
H | Hour, in 24-hour format, without leading zeros |
hh | hour, in 12-hour format, with leading zeros |
h | Hour, in 12-hour format, without leading zeros |
m | minutes without leading zeros |
mm | Minutes with leading zeros |
s | seconds without leading zeros |
ss | Description with leading zeros |
X | Unix timestamp |