How do I calculate the date in JavaScript three months prior to today?

ID : 20077

viewed : 16

Tags : javascriptdatejavascript

Top 5 Answer for How do I calculate the date in JavaScript three months prior to today?

vote vote

96

var d = new Date(); d.setMonth(d.getMonth() - 3); 

This works for January. Run this snippet:

var d = new Date("January 14, 2012");  console.log(d.toLocaleDateString());  d.setMonth(d.getMonth() - 3);  console.log(d.toLocaleDateString());


There are some caveats...

A month is a curious thing. How do you define 1 month? 30 days? Most people will say that one month ago means the same day of the month on the previous month citation needed. But more than half the time, that is 31 days ago, not 30. And if today is the 31st of the month (and it isn't August or Decemeber), that day of the month doesn't exist in the previous month.

Interestingly, Google agrees with JavaScript if you ask it what day is one month before another day:

Google search result for 'one month before March 31st' shows 'March 3rd'

It also says that one month is 30.4167 days long: Google search result for 'one month in days' shows '30.4167'

So, is one month before March 31st the same day as one month before March 28th, 3 days earlier? This all depends on what you mean by "one month before". Go have a conversation with your product owner.

If you want to do like momentjs does, and correct these last day of the month errors by moving to the last day of the month, you can do something like this:

const d = new Date("March 31, 2019");  console.log(d.toLocaleDateString());  const month = d.getMonth();  d.setMonth(d.getMonth() - 1);  while (d.getMonth() === month) {      d.setDate(d.getDate() - 1);  }  console.log(d.toLocaleDateString());

If your requirements are more complicated than that, use some math and write some code. You are a developer! You don't have to install a library! You don't have to copy and paste from stackoverflow! You can develop the code yourself to do precisely what you need!

vote vote

87

I recommend using a library called Moment.js.

It is well tested, works cross browser and on server side(I am using it both in Angular and Node projects). It has great support for locale dates.

http://momentjs.com/

var threeMonthsAgo = moment().subtract(3, 'months');  console.log(threeMonthsAgo.format()); // 2015-10-13T09:37:35+02:00 

.format() returns string representation of date formatted in ISO 8601 format. You can also use it with custom date format like this:.format('dddd, MMMM Do YYYY, h:mm:ss a')

vote vote

75

A "one liner" (on many line for easy read)) to be put directly into a variable:

var oneMonthAgo = new Date(     new Date().getFullYear(),     new Date().getMonth() - 1,      new Date().getDate() ); 
vote vote

64

To make things really simple you can use DateJS, a date library for JavaScript:

http://www.datejs.com/

Example code for you:

Date.today().add({ months: -1 }); 
vote vote

53

This should handle addition/subtraction, just put a negative value in to subtract and a positive value to add. This also solves the month crossover problem.

function monthAdd(date, month) {     var temp = date;     temp = new Date(date.getFullYear(), date.getMonth(), 1);     temp.setMonth(temp.getMonth() + (month + 1));     temp.setDate(temp.getDate() - 1);       if (date.getDate() < temp.getDate()) {          temp.setDate(date.getDate());      }      return temp;     } 

Top 3 video Explaining How do I calculate the date in JavaScript three months prior to today?

Related QUESTION?