120

Possible Duplicate:
Subtract days from a date in javascript

I have got a JavaScript that basically returns a date that is 2 days ago. It is as follows:

var x;
var m_names = new Array("January", "February", "March", 
    "April", "May", "June", "July", "August", "September", 
    "October", "November", "December");

var d = new Date();
var twoDaysAgo = d.getDate()-2;  //change day here
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var x = twoDaysAgo + "-" + m_names[curr_month] + "-" + curr_year;

document.write(x);

Assuming today is 12-December-2012, the above will return the date 10-December-2012. I don't think this will work dynamically as we move forward into a new month, OR, change the day from -2 to -15. It will work only from the 3rd of the month.

How can I modify this so when it is 12-December-2012 today and I want it to return me the date 15 days ago it should be 27-November-2012... and not -3-December-2012?

Any help appreciated. Thanks! I'm a Javascript newbie.

Community
  • 1
  • 1
dat789
  • 1,923
  • 3
  • 20
  • 26

2 Answers2

209

If you have a date object, you can set it to two days previous by subtracting two from the date:

var d = new Date();
d.setDate(d.getDate() - 2);
console.log(d.toString());

// First of month
var c = new Date(2017,1,1); // 1 Feb -> 30 Jan
c.setDate(c.getDate() - 2);
console.log(c.toString());

// First of year
var b = new Date(2018,0,1); // 1 Jan -> 30 Dec
b.setDate(b.getDate() - 2);
console.log(b.toString());
RobG
  • 142,382
  • 31
  • 172
  • 209
  • This returns milliseconds, you have to feed the result back into the Date constructor to get a date object. See @pfried answer below – Petrov Oct 04 '15 at 13:01
  • 4
    @Petrov—the [*setDate*](http://ecma-international.org/ecma-262/6.0/index.html#sec-date.prototype.setdate) method modifies the date object and returns the new timevalue. Passing that to the Date constructor needlessly creates a new Date with the same [*timevalue*](http://ecma-international.org/ecma-262/6.0/index.html#sec-date-value). – RobG Oct 05 '15 at 13:21
  • 5
    This answer is correct. It's important to note that if `getDate() - x` is 0 or negative, it will move to the previous month. @Petrov - `getDate()` does NOT operate on milliseconds. – McAden Oct 15 '15 at 21:45
  • What happens when the date is 'YYYY-MM-01' or 'YYYY-MM-01'? This will not work. – bpavlov Jun 01 '16 at 13:33
  • 4
    @bpavlov–see McAden's comment prior to yours. It works. – RobG Jun 01 '16 at 23:57
  • How is the first day of the month? i.e 1st Mar – Colin Wang Feb 20 '17 at 20:01
52

You can do the following

​var date = new Date();
var yesterday = date - 1000 * 60 * 60 * 24 * 2;   // current date's milliseconds - 1,000 ms * 60 s * 60 mins * 24 hrs * (# of days beyond one to go back)
yesterday = new Date(yesterday);
console.log(yesterday);​

The Date is available as a number in miliiseconds, you take today subtract two days and create a new date using that number of milliseconds

xinthose
  • 3,213
  • 3
  • 40
  • 59
pfried
  • 5,000
  • 2
  • 38
  • 71
  • 2
    I d pick this over the picked answer cause this takes care of overlap in months – Femi Oni Jul 29 '15 at 16:29
  • 3
    Actually, using the date module takes care of that too. – dougalg Aug 31 '15 at 13:32
  • 14
    @FemiOni—the accepted answer does that too. This answer assumes that all days are 24 hours long. Over daylight saving changes they are either 23 or 25 hours, so the above will provide an incorrect result in those cases. – RobG Oct 05 '15 at 13:23
  • Is this safe with respect to daylight saving time switches? I don't think so. – Cephalopod Oct 13 '20 at 07:03