Last active
August 29, 2015 14:06
-
-
Save meyerbaptiste/d5b0b658f64c8b0dd562 to your computer and use it in GitHub Desktop.
Get a date with an offset in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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