- 获取当前的时间(年-月-日)
getCurrentDay() {
var date = new Date()
var dd = String(date.getDate()).padStart(2, '0')
var mm = String(date.getMonth() + 1).padStart(2, '0')
var yyyy = date.getFullYear()
this.currentDay = yyyy + '-' + mm + '-' + dd
}
- 获取明天的时间(年-月-日)
getCurrentNextDay() {
var date = new Date()
var dd = String(date.getDate() + 1).padStart(2, '0')
var mm = String(date.getMonth() + 1).padStart(2, '0')
var yyyy = date.getFullYear()
this.currentNextDay = yyyy + '-' + mm + '-' + dd
}
- 获取本月最后一天,下月第一天
getMonthLastDay() {
let date = new Date()
let currentMonth = date.getMonth()
let nextMonth = ++currentMonth
let nextMonthDay = new Date(date.getFullYear(), nextMonth, 1)
this.nextMonthFirstDay = parseTime(new Date(date.getFullYear(), nextMonth, 1), '{y}-{m}-{d}') // 下月第一天
let oneDay = 1000 * 60 * 60 * 24
this.currentMonthEndDay = parseTime(new Date(nextMonthDay - oneDay), '{y}-{m}-{d}') // 本月最后一天
},
以上parseTime()
方法的使用
export function parseTime(time, cFormat) {
if (arguments.length === 0) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
time = parseInt(time)
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
return format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') {
return ['日', '一', '二', '三', '四', '五', '六'][value]
}
return value.toString().padStart(2, '0')
})
}