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 Date.now() 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(Date.now() + 1000 * nSecs));
}

console.log(
    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 Object
  3. JavaScript: Date.prototype
  4. JavaScript Date.parse()
  5. JavaScript: Date() Constructor
  6. JavaScript: Compute Date Range
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.