如何使用JavaScript获取昨天的日期

  • Post author:
  • Post category:java


Well, first you get the date at the current time (today), then you subtract a day from it:

好吧,首先获取当前时间(今天)的日期,然后从中减去一天:

const today = new Date()
const yesterday = new Date(today)

yesterday.setDate(yesterday.getDate() - 1)

today.toDateString()
yesterday.toDateString()

We use the

setDate()

method on

yesterday

, passing as parameter the current day minus one.

我们在

yesterday

使用

setDate()

方法,将当前日期减一作为参数传递。

Even if it’s day 1 of the month, JavaScript is logical enough and it will point to the last day of the previous month.

即使是每月的第一天,JavaScript也足够逻辑,它将指向上个月的最后一天。

翻译自:

https://flaviocopes.com/how-to-get-yesterday-date-javascript/