Results 1 to 3 of 3

Thread: Manipulating Dates

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

    Default Manipulating Dates

    Hi ya,

    I'm having trouble working with the date object. I'm trying to add seven to the current date, but if I do say something like:

    Code:
    var forwardDate = currentDate+7;
    That gives me the wrong date, if the date is greater than say 31, as then it won't be even a day in the calender.

    Is there a way to make sure that if the date rolls past 31 it'll go down to the 1st or something?

    I hope I make sense....

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You shouldn't add directly to the date, it'll cause odd behaviour. The Date object provides several methods used to set the current date; the easiest to use is the constructor function,
    Code:
    Date(/* String */ dateString)
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  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
    I'm having trouble working with the date object. I'm trying to add seven to the current date [...]
    Use the setDate method:

    Code:
    var nextWeek = new Date();
    
    nextWeek.setDate(nextWeek.getDate() + 7);
    Is there a way to make sure that if the date rolls past 31 it'll go down to the 1st or something?
    Date rollover is automatic if you take the approach above. The same goes for the other set* methods.

    Mike

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
  •