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. JS: Date.parse
  3. JS: Compute Date Range
  4. JS: Date Object
  5. JS: Date Constructor
  6. JS: Date.prototype
Liket it? 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 Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc