SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    add 5 days to a string

    I have this string
    var start = $('[name=invoicedate]').val();
    alert('start'); // 2010/01/01

    how can I add five days to have the following?
    alert('start'); // 2010/01/10

    Tried
    var start = new Date(year, month, day+5);
    but seems not work

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    1. Create a date.
    2. Add five days
    3. Profit???

    Assuming that $('[name=invoicedate]').val() is the string '2010/01/01', you can create a date using that string with:

    Code:
    var start = new Date($('[name=invoicedate]').val());
    Then you can use the getDate and setDate methods to add 5 days to it with:

    Code:
    start.setDate(start.getDate() + 5);
    resulting in the following code:

    Code javascript:
    var start = new Date($('[name=invoicedate]').val());
    start.setDate(start.getDate() + 5);

    If you want to retrieve just the date portion so you can display it elsewhere, you can use the toDateString method

    Code:
    var date = start.toDateString()
    // Wed Jan 06 1010
    Or, if you want to format it in some special way, you can use getDate, getMonth (which starts at 0) and getFullYear

    Code:
    var date = start.getFullYear() + '/' + (start.getMonth() + 1) + '/' + start.getDate();
    // 2010/01/06
    Last edited by paul_wilkins; Mar 8, 2011 at 20:20. Reason: Corrected syntax error
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul,

    Thanks for your reply and attention.
    Just want to ask something

    var start = new Date($('[name=invoicedate]').val());
    start.setDate(start.getDate() + 5);
    Until here it works perfect, it adds five days

    but this does not give me a string
    var date = start.getFullYear() + '/' + (start.getFullMonth + 1) + '/' + start.getDate; // 2010/01/06
    alert(date);
    // 2011/NaN/function getDate(){ [native code] } ??

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by pedroz View Post
    but this does not give me a string
    var date = start.getFullYear() + '/' + (start.getFullMonth + 1) + '/' + start.getDate; // 2010/01/06
    alert(date);
    // 2011/NaN/function getDate(){ [native code] } ??
    I should test before showing such code.

    Instead of getFullMonth it's getMonth(), and instead of getDate it's getDate()

    Code javascript:
    var date = start.getFullYear() + '/' + (start.getMonth() + 1) + '/' + start.getDate(); // 2010/01/06

    I'll update the initial post as well.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •