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)

JS Date

  1. JS: Date Tutorial
  2. JS: Date.parse
  3. JS: Compute Date Range
  4. JS: Date Object
  5. JS: Date Constructor
  6. JS: Date.prototype
Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Object Ref
  5. DOM Scripting
  6. SVG
  7. Blog