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


当前回答

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

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

其他回答

这里有2个一句话:

new Date(new Date().setHours(-1))
new Date(Date.now() - 86400000)

试试这个

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

[2022年4月]:下面是一个扩展Date原型的代码片段(不污染全局命名空间)

[edit sept 2020]:一个包含之前答案的片段,并添加了一个箭头函数。

// a (not very efficient) oneliner let yesterday = new Date(new Date().setDate(new Date().getDate()-1)); console.log(`Yesterday (oneliner)\n${yesterday}`); // a function call yesterday = ( function(){this.setDate(this.getDate()-1); return this} ) .call(new Date); console.log(`Yesterday (function call)\n${yesterday}`); // an iife (immediately invoked function expression) yesterday = function(d){ d.setDate(d.getDate()-1); return d}(new Date); console.log(`Yesterday (iife)\n${yesterday}`); // oneliner using es6 arrow function yesterday = ( d => new Date(d.setDate(d.getDate()-1)) )(new Date); console.log(`Yesterday (es6 arrow iife)\n${yesterday}`); // use a method const getYesterday = (dateOnly = false) => { let d = new Date(); d.setDate(d.getDate() - 1); return dateOnly ? new Date(d).toDateString() : d; }; console.log(`Yesterday (method)\n${getYesterday()}`); console.log(`Yesterday (method dateOnly=true)\n${getYesterday(true)}`); // use Date.now console.log(`Yesterday, using Date.now\n${new Date(Date.now() - 864e5)}`); .as-console-wrapper { max-height: 100% !important; }

如果你既想获取昨天的日期,又想将日期格式化为人类可读的格式,可以考虑创建一个自定义DateHelper对象,看起来像这样:

var DateHelper = { addDays : function(aDate, numberOfDays) { aDate.setDate(aDate.getDate() + numberOfDays); // Add numberOfDays return aDate; // Return the date }, format : function format(date) { return [ ("0" + date.getDate()).slice(-2), // Get day and pad it with zeroes ("0" + (date.getMonth()+1)).slice(-2), // Get month and pad it with zeroes date.getFullYear() // Get full year ].join('/'); // Glue the pieces together } } // With this helper, you can now just use one line of readable code to : // --------------------------------------------------------------------- // 1. Get the current date // 2. Subtract 1 day // 3. Format it // 4. Output it // --------------------------------------------------------------------- document.body.innerHTML = DateHelper.format(DateHelper.addDays(new Date(), -1));

(也可以参看这把小提琴)

下面是一个一行程序,用于在文本中获取YYYY-MM-DD格式的昨天日期并处理时区偏移。

new Date(Date.now() - 1 * 864e5 - new Date(Date.now() - 1 * 864e5).getTimezoneOffset() * 6e4).toISOString().split('T')[0]

它显然可以改变为返回日期,x天以前。包括时间等。

console.log(日期()) console.log(new Date(Date.now() - 1 * 864e5 - new Date(Date.now() - 1 * 864e5).getTimezoneOffset() * 6e4).toISOString().split('T')[0]);/ /“2019-11-11” console.log(new Date(Date.now() - 1 * 864e5 - new Date(Date.now() - 1 * 864e5). gettimezoneoffset () * 6e4). toisostring ().split('.')[0]. log .log(new Date(Date.now() - 1 * 864e5)替换(' T ', ' '));// "2019-11-11 11:11:11" // that is: [dates] * 24 * 60 * 60 * 1000 - offsetinmin * 60 * 1000 //这是:[dates] * 24 * 60 * 60 * 1000 - offsetinmin * 60 * 1000