JavaScript: Compute Date Range

By Xah Lee. Date: . Last updated: .

The best way to compute date range is to convert time to epoch milli-seconds (returned by or date.getTime()) then add or minus by milli-seconds, then convert it back to whichever time format you want.

function nSecsInFuture(nSecs) {
    // returns a date object that's n secs in the future
    return (new Date( + 1000 * nSecs));

    nSecsInFuture(60) // 1 min in future

// sample output
// Tue Apr 26 2016 18:07:02 GMT-0700 (PDT)

Date Topic

  1. JavaScript: Date Tutorial
  2. JavaScript Date.parse
  3. JavaScript: Compute Date Range

  1. JavaScript: Date Object
  2. JavaScript: Date Constructor
  3. JavaScript: Date.prototype
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.