Last active
December 18, 2015 02:58
-
-
Save manufitoussi/5714523 to your computer and use it in GitHub Desktop.
Method to add some days to a date. Takes into account any timezone offset.
This file contains hidden or 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
/** | |
* Adds nb days to the specified date. | |
* @param {Date} the date. | |
* @param {Int} the number of days you want to add (or remove if negative). | |
* @return {Date} the shifted date. | |
*/ | |
var addDays = function(date, nb) { | |
// day duration in ms. | |
var DAY = 1000*60*60*24; | |
// shifted date. | |
var result = new Date(date.getTime() + nb * DAY); | |
// timezone offset of date. | |
var dateOffset = date.getTimezoneOffset(); | |
// timezone offset of the result. | |
var resultOffset = result.getTimezoneOffset(); | |
//fixes the hour of the result due by a timezone difference. | |
result = new Date(result.getTime() - (dateOffset- resultOffset)*60*1000); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment