PDA

View Full Version : Date Help



gingerj
12-29-2005, 10:48 AM
Hi all, I've been constructing a web interface to produce link urls automatically (dont ask the boss wants it).

But I've got a bit stuck...

I'm using the Javascript Date function to automatically load the current day/month/year into some text boxes.

Now I need to add a week(+7) onto the current dates, and fill in another set of day/month/year text boxes. But when I do:

document.linkbuilder.dday.value = theday.getDate()+7;

It's not rolling the dates over, so todays date is the 29th and I'm getting 36. So how do I work this problem out cos I'm no Javascript programmer by any means.

I think I need to:
1) Get the current date + 7
2) If it's over 28/30/31 then take new date and get the difference
3) with the difference add it to 1 so get the proper date....?

How?

12345c
12-29-2005, 01:12 PM
All is simple if to count in milliseconds:

<script>
D=new Date();
alert(new Date(D.getTime()+86400*7*1000).getDate());
</script>

mwinter
12-29-2005, 08:13 PM
Now I need to add a week(+7) onto the current dates, and fill in another set of day/month/year text boxes. But when I do:

document.linkbuilder.dday.value = theday.getDate()+7;

It's not rolling the dates over [...]The various get* methods only return numbers. The roll-over behaviour only occurs when a Date object is initialised with out-of-range operands, or when an out-of-range value is used with a set* method.



myDate.setDate(myDate.getDate() + 7);
After that method call, you may now re-examine the modified object.



new Date(D.getTime()+86400*7*1000).getDate()Altering a date using milliseconds may cause problems: where daylight savings are used, not all days are the same length. If a date is being changed by days, months, or years, use the appropriate set* methods.

Mike

12345c
12-29-2005, 08:34 PM
> problems: where daylight savings are used
If it was so, JS would allow a mistake in the summer (in 1 hour)

mwinter
12-29-2005, 10:04 PM
If it was so, JS would allow a mistake in the summer (in 1 hour)It will:



var D = new Date(2005, 9, 29, 12), /* Sat., 29 Oct. 2005 12:00 */
Dp = new Date(+D + 864e5); /* Sun., 30 Oct. 2005 11:00 */

D.setDate(D.getDate() + 1); /* Sun., 30 Oct. 2005 12:00 */
As you can see, there is a difference between the resulting times. Whilst the millisecond addition will advance the Date object by twenty-four hours, the next civil days is actually in twenty-five hours time due to the DST transition.

When people consider 'tomorrow' or a 'week from now', they are thinking in civil days not precisely twenty-four or 168 hours.

Mike

12345c
12-30-2005, 10:38 AM
Oh, thanks for an example, seconds really flow in regular intervals, but readout of hours is displaced. Your example is completely correct