Skip to content

Instantly share code, notes, and snippets.

@manufitoussi
Last active December 18, 2015 02:58
Show Gist options
  • Save manufitoussi/5714523 to your computer and use it in GitHub Desktop.
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.
/**
* 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