8

If I have a date that is 2011-01-02 and I subtract 7 days from that date it should give me 2010-12-26, but instead it gives me 2011-01-26?

See the JS below to verify with link:

var date = new Date('2011','01','02');
alert('the original date is '+date);
var newdate = new Date(date);
newdate = newdate.setDate(newdate.getDate() - 7);
var nd = new Date(newdate);
alert('the new date is '+nd);

http://jsbin.com/upeyu/6

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Amir
  • 2,249
  • 7
  • 34
  • 48

5 Answers5

20

I think you meant to do this: (working perfectly)

var date = new Date('2011','01','02');
alert('the original date is '+date);
var newdate = new Date(date);
newdate.setDate(newdate.getDate() - 7);
var nd = new Date(newdate);
alert('the new date is '+nd);

jsFiddle example

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • the year should be 2010, and the month should be december. – Amir Dec 01 '10 at 00:13
  • Oops, deleted my comment by accident :( +1, this works because Javascript knows how to deal with negative dates - February the -5th – Gareth Dec 01 '10 at 00:16
  • 1
    Actually I think I am confused. Isn't 2011-01-02 = Jan. 2, 2011. Why is it Feb 2, 2011? – Amir Dec 01 '10 at 00:17
  • 4
    Javascript months are zero-indexed: 0 = January. Go figure! – Gareth Dec 01 '10 at 00:18
  • I just needed the actual number date from the calendar, so this is what I ended up doing... http://jsfiddle.net/dennismonsewicz/ghqhP/ – dennismonsewicz Mar 19 '12 at 15:44
  • the problem is when the number is in a var than you have to use ... newdate.setDate(newddate.getDate() - parseInt(mynumber)); – Try it Sep 16 '16 at 09:54
4

getDate() and setDate() both refer to only the day of the month part of the date. In order to subtract 7 days you want to do this:

myDate.setDate( myDate.getDate() - 7 );

This sets the day of the month to the day of the month minus seven. If you end up using a negative number it goes back to the previous month.

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
2

.getDate() only returns the day of the month, and .setDate() only sets the DAY of the month, not the date.

Try doing

var date = new Date('2011','01','02');
alert('the original date is '+date);
var newdate = new Date(date.getTime() - 604800000);
alert('the new date is '+newdate);
simshaun
  • 21,263
  • 1
  • 57
  • 73
0

i have written a utility program Date.prototype.subDuration = subDuration; function subDuration(a,b) { if ((typeof a === 'string')&&(typeof b === 'number')){ if ((a ==="Add") || (a ==="Sub")){ subdur.call(this,a,b) }else{ return false; } }

                function subdur(action,days){
                   switch (action){
                      case 'Add': 
                      addDays.call(this,days);
                      break;
                      case 'Sub': 
                      rmvDays.call(this,days)
                      break;
                      default:
                      return false;
                   }
                   function addDays(days){
                        this.setDate(this.getDate()+days)
                   };
                   function rmvDays(days){
                        this.setDate(this.getDate()-days);
                   };
                }


            }

            var d = new Date('2011','00','02');
            alert(d);
            d.subDuration('Add',2);
            alert(d);
            d.subDuration('Sub',3);
            alert(d);
Gaurav
  • 821
  • 9
  • 11
0

This is because the setDate method is only supposed to set the day of the month

Gareth
  • 133,157
  • 36
  • 148
  • 157
  • Ohhh. Thanks. Do you know what method I need to use to make it work correctly for months and years? – Amir Dec 01 '10 at 00:10
  • 1
    You can perform date arithmetic by adding/subtracting numbers of milliseconds, so `new Date(date - (7 * 1000 * 60 * 60 * 24)) // 7 days ago`. It's clunky, yes, but it's the only native way – Gareth Dec 01 '10 at 00:12
  • Sorry, this answer might be correct but it doesn't fully answer the question because Javascript is smarter than I make it sound - see Jacob Relkin's answer – Gareth Dec 01 '10 at 00:18
  • @Gareth Strange. When I use setDate() and getDate() and minus number of days, the month is also subtracted by 1 even though the month should remain same say July 15 - 5 => June 10. But using the native way works as it should ! – thethakuri Jun 02 '16 at 05:31