How to get yesterday date in node.js backend?

后端 未结 8 1109
误落风尘
误落风尘 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;
    }
    

提交回复
热议问题