JS: 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. JS: Date Tutorial
  2. JavaScript Date.parse
  3. JS: Compute Date Range

  1. JS: Date Object
  2. JS: Date Constructor
  3. JS: Date.prototype
Liket it? Put $1 at patreon.

Or, Buy JavaScript in Depth