一、当前时间的前一个小时
var frontOneHour = new Date(new Date().getTime() - 1 * 60 * 60 * 1000);
console.log(new Date(new Date().getTime() - 1 * 60 * 60 * 1000), new Date())
console.log(frontOneHour)
二、格式化时间
function frontOneHour (fmt) {
var currentTime = new Date(new Date().getTime())
console.log(currentTime)
var o = {
'M+': currentTime.getMonth() + 1,
'd+': currentTime.getDate(),
'h+': currentTime.getHours(),
'm+': currentTime.getMinutes(),
's+': currentTime.getSeconds(),
'q+': Math.floor((currentTime.getMonth() + 3) / 3),
'S': currentTime.getMilliseconds()
}
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (currentTime.getFullYear() + '').substr(4 - RegExp.$1.length))
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
}
return fmt
}
frontOneHour('yyyy-MM-dd hh:mm:ss')
frontOneHour('yyyy-MM-dd')
frontOneHour('yyyyMMDD')