How to get yesterday date in node.js backend?

2019-03-30 09:32发布

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 or some other way I want yesterday date. Still I did't get any proper method. For the time being I am calculating yesterday date manually with lot of code. Is there any other method other then writing manually ?

7条回答
来,给爷笑一个
2楼-- · 2019-03-30 10:04

I would take a look at moment.js if you are interested in doing calculations with dates, there are many issues you can run into trying to do it manually or even with the built in Date objects in JavaScript/node.js such as leap years and daylight savings time issues.

http://momentjs.com/

For example:

var moment = require('moment');
var yesterday = moment().subtract(1, 'days');
console.log(yesterday.format());
查看更多
登录 后发表回答