用 JavaScript 计算昨天的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5511323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:32:48  来源:igfitidea点击:

Calculate the date yesterday in JavaScript

javascriptdate

提问by Omega

How can I calculate yesterdayas a date in JavaScript?

如何在 JavaScript 中将昨天计算为日期?

回答by James Kyburz

var date = new Date();

date ; //# => Fri Apr 01 2011 11:14:50 GMT+0200 (CEST)

date.setDate(date.getDate() - 1);

date ; //# => Thu Mar 31 2011 11:14:50 GMT+0200 (CEST)

回答by KooiInc

Not very efficient, but as oneliner:

效率不高,但作为oneliner:

var yesterday = new Date(new Date().setDate(new Date().getDate()-1));


The above creates three Dateobjects which is needlessly wasteful. This can be reduced to a single instantiation with:

以上创建了三个Date不必要的浪费的对象。这可以简化为单个实例:

var yesterday = (function(){this.setDate(this.getDate()-1); return this})
                  .call(new Date)

Or, if you prefer:

或者,如果您更喜欢:

var yesterday = (function(d){ d.setDate(d.getDate()-1); return d})(new Date)

Or, if you prefer ES6 with arrow function:

或者,如果您更喜欢带有箭头功能的 ES6:

let yesterday = ( d => new Date(d.setDate(d.getDate()-1)) )(new Date);

回答by ashishjmeshram

Try this

尝试这个

var d = new Date();
d.setDate(d.getDate() - 1);

回答by Fabiano Soriani

Surprisingly no answer point to the easiest cross browser solution

令人惊讶的是,最简单的跨浏览器解决方案没有答案

To find exactly the same time yesterday:

要查找与昨天完全相同的时间:

var yesterday = new Date(Date.now() - 86400000); // that is: 24 * 60 * 60 * 1000

That is if you wanna go dependency free, otherwise I'd recommend using http://momentjs.com

那就是如果你想无依赖,否则我建议使用http://momentjs.com

回答by Ami Heines

To generalize the question and make other diff calculations use:

要概括问题并进行其他差异计算,请使用:

var yesterday = new Date((new Date()).valueOf() - 1000*60*60*24);

this creates a new date object based on the value of "now" as an integer which represents the unix epoch in milliseconds subtracting one day.

这将根据作为整数的“now”值创建一个新的日期对象,该整数表示以毫秒为单位减去一天的 unix 纪元。

Two days ago:

前两天:

var twoDaysAgo = new Date((new Date()).valueOf() - 1000*60*60*24*2);

An hour ago:

一小时前:

var oneHourAgo = new Date((new Date()).valueOf() - 1000*60*60);

回答by guyaloni

I use momentlibrary, it is very flexible and easy to use.

我使用的是moment库,它非常灵活且易于使用。

In your case:

在你的情况下:

let yesterday = moment().add(-1, 'day').toDate();

====== UPDATE ======

====== 更新 ======

let yesterday = moment().subtract(1, 'day').toDate();

回答by Gopinath Radhakrishnan

new Date(new Date().setDate(new Date().getDate()-1))

回答by Billyhomebase

//Create a date object using the current time
var now = new Date();

//Subtract one day from it
now.setDate(now.getDate()-1);

回答by Deepu Reghunath

var today = new Date();
var yesterday1 = new Date(new Date().setDate(new Date().getDate() - 1));
var yesterday2 = new Date(Date.now() - 86400000);
var yesterday3 = new Date(Date.now() - 1000*60*60*24);
var yesterday4 = new Date((new Date()).valueOf() - 1000*60*60*24);
console.log("Today: "+today);
console.log("Yesterday: "+yesterday1);
console.log("Yesterday: "+yesterday2);
console.log("Yesterday: "+yesterday3);
console.log("Yesterday: "+yesterday4);

回答by Evgeni Makarov

This will produce yesterday at 00:00 with minutes precision

这将在昨天 00:00 以分钟精度产生

var d = new Date();
d.setDate(d.getDate() - 1);
d.setTime(d.getTime()-d.getHours()*3600*1000-d.getMinutes()*60*1000);