I am trying to get the last hour, last month and yesterday from the current date and time, I use the Date () object to get the current date and time:
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); }
I need a function that will return not only the date or time, but also the full datetime, for example:
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'
I also want to ask, what if the hour is 00:00:00 , what would be the result of the last hour? The same thing happens with the month and date.
source share