Design & Download
Your JS Library
in Seconds!!!

Snippet: offsetDate()

Written by Christopher West (cwest) on December 11, 2015.
Create a new date object by specifying an offset in seconds or an offset based on what the current time is in a different environment.
function offsetDate(opt_date, offset) {
  if (arguments.length < 2) {
    offset = opt_date;
    opt_date = new Date;
  }
  return new Date(+opt_date + (typeOf(offset, 'Date') ? +new Date - offset : +offset));
}

offsetDate(...) API Documentation

Description

Create a new date object by specifying an offset in seconds or an offset based on what the current time is in a different environment.

Parameters

  1. opt_date {Date=}:
    Optional. Defaults to the current date/time (new Date()). The date to copy and offset.
  2. offset {Date|number}:
    If a date is specified, this will be used to generate an offsetted version of opt_date, calculating the difference between this and the actual current time and then adding that to opt_date. If a number is specified, this will be treated as the number of milliseconds to add to opt_date to create the new one.

Returns

Returns an offsetted version of opt_date.