from Date I just want to subtract 1 day in javascript/angularjs

前端 未结 3 1429
暖寄归人
暖寄归人 2021-02-04 09:05

with the help of new Date() how i can achieve this. my code :

var temp =new Date(\"October 13, 2014 22:34:17\");
console.log(new Date(temp-1));

3条回答
  •  我寻月下人不归
    2021-02-04 09:52

    The simple answer is that you want to subtract a days worth of milliseconds from it. So something like the following

    var today = new Date('October 13, 2014 22:34:17');
    var yesterday = new Date(today.getTime() - (24*60*60*1000));
    console.log(yesterday);
    

    The problem with this is that this really gives you 24 hours earlier, which isn't always a day earlier due to things such as changes in Daylight Saving Time. If this is what you want, fine. If you want something more sophisticated, check out moment.js

提交回复
热议问题