我正在尝试从当前日期和时间获取最后一个小时、上个月和昨天,我正在使用 Date() 对象来获取当前日期和时间:
function fetchCurrentDateTime() {
var currentDateobj = new Date()
var Day = currentDateobj.getDate()
var Hour = currentDateobj.getHours()
var Month = 1+(currentDateobj.getMonth())
var Year = currentDateobj.getFullYear()
console.log('Today is:', Day+'-'+Month+'-'+Year);
console.log('yesterday was:', Day-1+'-'+Month+'-'+Year);
console.log('Its', Hour, 'hrs');
console.log('It was', Hour-1, 'an Hour back.');
console.log('This is:', Month, 'Month');
console.log('It was', Month-1, 'Month, a month ago.');
console.log('It is', Year);
}
我想要一个函数,它不仅会返回日期或时间,还会返回完整的日期时间,例如:
Today: '2013-05-21 10:06:22'
Yesterday: '2013-05-20 10:06:22'
Current Hour: '2013-05-21 10:06:22'
Last Hour: '2013-05-21 09:06:22'
Current Month: '2013-05-21 10:06:22'
Last Month: '2013-04-21 10:06:22'
我还想问一下,如果 hour 是00:00:00
,那么 last hour 的结果会是什么?月份和日期也是如此。