vue 获取当前时间的前一天 后一天 一周 或者是当前时间的几个小时
一、在方法中写入以下代码:methods: {getDay (day, hours) {var today = new Date()var targetday = today.getTime() + 1000 * 60 * 60 * 24 * day + hourstoday.setTime(targetday)var tYear = today.getFullYear()var tMonth =
·
一、在方法中写入以下代码:
methods: {
getDay (day, hours) {
var today = new Date()
var targetday = today.getTime() + 1000 * 60 * 60 * 24 * day + hours
today.setTime(targetday)
var tYear = today.getFullYear()
var tMonth = today.getMonth()
var tDate = today.getDate()
var getHours = today.getHours()
tMonth = this.doHandleMonth(tMonth + 1)
tDate = this.doHandleMonth(tDate)
return tYear + '-' + tMonth + '-' + tDate + '小时:' + getHours
},
doHandleMonth (month) {
var m = month
if (month.toString().length === 1) {
m = '0' + month
}
return m
},
}
二、测试:
mounted () {
console.log('昨天:', this.getDay(-1, 7200000))
console.log('今天:', this.getDay(0, 3600000))
console.log('明天:', this.getDay(1, 3600000))
console.log('一周后:', this.getDay(7, 7200000)
}
效果:
更多推荐
已为社区贡献2条内容
所有评论(0)