Skip to content

Instantly share code, notes, and snippets.

@whroman
Created May 1, 2014 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whroman/2aa7da8550893d114c64 to your computer and use it in GitHub Desktop.
Save whroman/2aa7da8550893d114c64 to your computer and use it in GitHub Desktop.
Extending Javascript's Date.prototype
Date.prototype.getDayofYear = function(){
var d= new Date(this.getFullYear(), 0, 0);
return Math.floor((this-d)/8.64e+7);
}
Date.prototype.addDays = function (n) {
var year = this.getFullYear();
var month = this.getMonth();
var date = this.getDate() + n;
newDate = new Date(year, month, date);
return newDate;
}
Date.prototype.occuredAfter = function (dateObj) {
var diffMS = this.getTime() - dateObj.getTime();
var occured = diffMS >= 0 ? true : false;
return occured;
}
Date.prototype.getDaysSince = function (dateObj) {
var oneDayMS = 1000 * 60 * 60 * 24;
var diffMS = this.getTime() - dateObj.getTime();
var diffDays = Math.round(diffMS/oneDayMS);
return diffDays
}
@whroman
Copy link
Author

whroman commented May 1, 2014

Useful methods I've found/developed for extending Javascript's native Date object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment