How to Add Days to a Date in JavaScript

Written by kevin lee
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Add Days to a Date in JavaScript
Dates are critical entities in Web-based computer programs. (valentine's day calendar image by Wolszczak from Fotolia.com)

JavaScript date manipulation is not as robust and full-featured as the date-handling methods found in higher level programming languages such as PHP and ASP.NET. If you need to format a date in JavaScript or perform date arithmetic, you must manually manipulate objects and use various built-in functions. Developers frequently add Web pages to their websites that require date addition. Add days to dates on your site by creating a generic JavaScript date handler.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Open one of your Web pages in Notepad or in an HTML editor.

  2. 2

    Add the following JavaScript function to the <script> section of the document:

    function add Date(date Object, numDays) {

    dateObject.setDate(dateObject.getDate() + numDays);

    return dateObject.toLocaleDateString();

    }

    This function accepts a date object and a numDays variable as parameters. The function then calls the set Date method that adds the value stored in numDays to the date object. Finally, the function converts the incremented date object from milliseconds to a readable format using the toLocaleDateString method.

  3. 3

    Add the following code below the function:

    var numDays;

    var old Date;

    var new Date;

    / Add 10 days to the current date /

    old Date = new Date();

    numDays = 10;

    document.write("About to add " + numDays + " days to " + oldDate.toLocaleDateString() + "");

    new Date = add Date(old Date, numDays)

    document.writeln("New Date = " + new Date + "");

    / Add two days to a specific date /

    old Date = new Date("December 13, 1988");

    numDays = 2;

    document.write("About to add " + numDays + " days to " + oldDate.toLocaleDateString() + "");

    new Date = add Date(old Date, numDays)

    document.writeln("New Date = " + new Date + "");

    This code tests the add Date function. The first block of code passes the current date to the function and 10 as the number of days to add to the current date. The second block of code demonstrates the function's ability to handle any date such as December 13, 1988.

  4. 4

    Save the document and open it in your browser. The code will print the original dates and the new dates resulting from the addition of days to the original dates. Give the add Date function any name that is meaningful to you.

Tips and warnings

  • If you pass the function more than the number of days in a month, JavaScript will increment the month by one and display the correct day, month and year. See Resources for a list of ways to create new date objects.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.