- 获取当前时间
new Date() - 获取当前时间的时间戳
new Date().getTime() - 获取当前时间的时分秒
第一种方法:
`先获取当前时间`
const time = new Date()
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
第二种方法:
`引入moment`
import moment from 'moment'
export function formatHourForMoment(time, format = "HH:mm:ss") {
return moment(time).format(format)
}
- 获取当前时间的00:00:00和23:59:59
//当前时间23:59:59
new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1
//当前时间的00:00:00
new Date(new Date().toLocaleDateString()).getTime()
toLocaleDateString() 根据本地时间把 Date 对象的日期部分转换为字符串:
var d=new Date();
var n=d.toLocaleDateString();