Skip to content

Instantly share code, notes, and snippets.

@meyerbaptiste
Last active August 29, 2015 14:06
Show Gist options
  • Save meyerbaptiste/d5b0b658f64c8b0dd562 to your computer and use it in GitHub Desktop.
Save meyerbaptiste/d5b0b658f64c8b0dd562 to your computer and use it in GitHub Desktop.
Get a date with an offset in JS
'use strict';
var getDateWithOffset = function getDateWithOffset(date, offset) {
if (!(date instanceof Date)) {
throw new TypeError('The parameter "date" must be a Date object.');
}
if (typeof offset !== 'number') {
throw new TypeError('The parameter "offset" must be a number.');
}
var localTime = date.getTime();
var localOffset = date.getTimezoneOffset() * 60000;
var utcTime = localTime + localOffset;
var distantTime = utcTime + (3600000 * offset);
return new Date(distantTime);
};
// Ex: var paris = getDateWithOffset(new Date(), +2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment