4

Hi I am trying to create a variable today that is the current date today. I am trying to add 106 days to it which works successfully. Then I am trying to create a second variable today2 and subtract 31 days from the 'today' variable (current date + 106 -31). This part is not working. This is what it is giving me...

Thu Mar 28 11:52:21 EDT 2013
Tue Nov 27 11:52:21 EST 2012

The second line is not 31 days before the first line. Can someone help me correct this?

Feel free to play with my jsfiddle http://jsfiddle.net/fjhxW/

<div id="current"></div>
<div id="current2"></div>
<div id="current3"></div>

var today = new Date();
var today2 = new Date();

today.setDate(today.getDate() + 106);

today2.setDate(today.getDate() - 31);  

var dd = today.getDate();
var mm = today.getMonth(); //January is 0!
var yy = today.getFullYear();

document.getElementById('current').innerHTML = today;
document.getElementById('current2').innerHTML = today2;
Naftali
  • 144,921
  • 39
  • 244
  • 303

4 Answers4

4

it's Xmas time so I give the answer just to copy/paste:

var oneDay = 24 * 60 * 60 * 1000, // 24h
    today = new Date().getTime(), // in ms
    firstDate,
    secondDate;

firstDate = new Date(today + 106 * oneDay);
secondDate = new Date(firstDate.getTime() - 31 * oneDay);
Eru
  • 675
  • 4
  • 8
  • Beware with daylight savings change days. One will have 23 hours and the other one 25. You could obtain strange results on that days. – Bardo Dec 12 '12 at 17:13
  • 1
    Yes. If you want to manipulate dates, work in timestamps! :-) – gen_Eric Dec 12 '12 at 17:16
2

try datejs:

Date.parse('t - 31 d'); // today - 31 days
Date.today().add(106).days().add(-31).days();
lante
  • 7,192
  • 4
  • 37
  • 57
  • Beat me to it by a couple of minutes ;) - I like this library because I don't want to worry about date arithmetic. It has leap year support, daylight savings, timezones etc. so you can `addDays`, `addMonths` and `addYears` very easily (or subtract them). – mccannf Dec 12 '12 at 17:28
  • but adding the plugin just to use it once is not a good idea. – Eru Dec 12 '12 at 17:29
  • @Eru, true, but I usually find if I'm manipulating dates in one part of my code I need to do it elsewhere... It's up to you whether the 30KB file is worth it or not. – mccannf Dec 12 '12 at 17:33
  • right, but i really dont know how many times will be used this function. mine was just an option or a suggestion, and you can take it or no. – lante Dec 12 '12 at 17:33
  • @mccannf agree, I looked into the codebase of date.js and I have to say it's idiotproof. So It's a good plugin, but if you know what you do is it really needed? – Eru Dec 12 '12 at 17:38
  • @Eru, another good point. I think we're drifting into the domain of [programmers stackexchange](http://programmers.stackexchange.com/questions/29513/is-reinventing-the-wheel-really-all-that-bad) ;) – mccannf Dec 12 '12 at 17:54
  • @mccannf, we do... a bit. In this particular example we got one function against the whole library :) – Eru Dec 12 '12 at 19:03
0

You cannot pass a negative number to setDate. setDate is used to set the date to set the absolute day, not relative days.

From the docs:

If the parameter you specify is outside of the expected range, setDate attempts to update the date information in the Date object accordingly. For example, if you use 0 for dayValue, the date will be set to the last day of the previous month.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

A mathemathical solution:

Add 75 days to your current day (106 - 31), then add 31 days to that date. Change the order in what you are showing both dates on your code.

Why go forward and backward when you can always go forward?

Bardo
  • 2,470
  • 2
  • 24
  • 42