How to get yesterday date in node.js backend?

后端 未结 8 974
误落风尘
误落风尘 2021-02-09 14:00

I am using date-format package in node back end and I can get today date using

var today = dateFormat(new Date());

In the same

相关标签:
8条回答
  • 2021-02-09 14:44

    To get the string in a format familiar to people

    // Date String returned in format yyyy-mm-dd
    function getYesterdayString(){
        var date = new Date();
        date.setDate(date.getDate() - 1);
        var day = ("0" + date.getDate()).slice(-2);
        var month = ("0" + (date.getMonth() + 1)).slice(-2); // fix 0 index
        return (date.getYear() + 1900) + '-' + month + '-' + day;
    }
    
    0 讨论(0)
  • 2021-02-09 14:46

    you can also change Hour,Minute,seconds and milliseconds attributes of time object like this.

    var date = new Date();
        date.setDate(date.getDate()-1);
        date.setHours(hour);
        date.setMinutes(minute);
        date.setSeconds(seconds);
        date.setMilliseconds(milliseconds);
    
    0 讨论(0)
提交回复
热议问题