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;
0

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Beware with daylight savings change days. One will have 23 hours and the other one 25. You could obtain strange results on that days.
Yes. If you want to manipulate dates, work in timestamps! :-)
2

try datejs:

Date.parse('t - 31 d'); // today - 31 days
Date.today().add(106).days().add(-31).days();

7 Comments

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).
but adding the plugin just to use it once is not a good idea.
@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.
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.
@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?
|
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.

2 Comments

@Neal: That's kinda what he's doing. Problem is when the value is a negative number.
@StackOverFlow: No one suggested using jQuery.
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?

4 Comments

Problem is, setDate is used to get the day, from 1-31. It's not for adding days.
But it will correctly address values out of the expected range, so you actually can use it for adding days.
I'm not sure if that behavior is defined though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.