最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

date - How to get last hour, last month and yesterday in javascript? - Stack Overflow

programmeradmin2浏览0评论

I am trying to get the last hour, last month and yesterday from current date and time, I am using Date() Object to get 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 want a function which would return me not only date or time but complete datetime like:

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 hour is 00:00:00, what would be the result of last hour then? Same is with month and date.

I am trying to get the last hour, last month and yesterday from current date and time, I am using Date() Object to get 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 want a function which would return me not only date or time but complete datetime like:

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 hour is 00:00:00, what would be the result of last hour then? Same is with month and date.

Share Improve this question edited May 22, 2013 at 5:36 MHS asked May 22, 2013 at 5:13 MHSMHS 2,35012 gold badges33 silver badges46 bronze badges 2
  • 1 You mean like this?: jsfiddle.net/dGvc3 – Ian Commented May 22, 2013 at 5:25
  • I mean to say that I don't want to make strings like year+'-'+month+'-'+day – MHS Commented May 22, 2013 at 5:28
Add a comment  | 

1 Answer 1

Reset to default 20

var today = new Date();
var yesterday = new Date(today.getTime() - (1000*60*60*24));
var hourago = new Date(today.getTime() - (1000*60*60));
    
console.log("Today:", today);
console.log("Yesterday:",yesterday);
console.log("Hours Ago:", hourago);

getTime() returns the timestamp of your Date object. You can then just substract the proper number of milliseconds and make a new object from that number. If needed, you can now format the dates in a way that you wish.

发布评论

评论列表(0)

  1. 暂无评论