Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the date yesterday in JavaScript

How can I calculate yesterday as a date in JavaScript?

like image 230
Omega Avatar asked Apr 01 '11 09:04

Omega


People also ask

How can I get yesterday's date?

How do you get yesterdays' date using JavaScript? We use the setDate() method on yesterday , passing as parameter the current day minus one. 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.

How do I get yesterday's timestamp?

You can get yesterday's Date by following approach Answered by Jiger Joshi. And by using new Timestamp(java. util. Date) you can get yesterday's timestamp, you should use Timestamp#equals to equaling two different timestamp.

Is date in past JavaScript?

To check if a date is in the past: Use the Date() constructor to get the current date. Optionally set the time of the current date to midnight.


1 Answers

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) 
like image 138
James Kyburz Avatar answered Oct 05 '22 15:10

James Kyburz