Results 1 to 6 of 6

Thread: Date Help

  1. #1
    Join Date
    Dec 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Date Help

    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?

  2. #2
    Join Date
    Dec 2005
    Location
    Moscow, Russia
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    All is simple if to count in milliseconds:
    Code:
    <script>
    D=new Date();
    alert(new Date(D.getTime()+86400*7*1000).getDate());
    </script>

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by gingerj
    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.

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


    Quote Originally Posted by 12345c
    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

  4. #4
    Join Date
    Dec 2005
    Location
    Moscow, Russia
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    > problems: where daylight savings are used
    If it was so, JS would allow a mistake in the summer (in 1 hour)

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by 12345c
    If it was so, JS would allow a mistake in the summer (in 1 hour)
    It will:

    Code:
    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

  6. #6
    Join Date
    Dec 2005
    Location
    Moscow, Russia
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh, thanks for an example, seconds really flow in regular intervals, but readout of hours is displaced. Your example is completely correct

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •