Created
May 1, 2014 00:01
-
-
Save whroman/2aa7da8550893d114c64 to your computer and use it in GitHub Desktop.
Extending Javascript's Date.prototype
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful methods I've found/developed for extending Javascript's native Date object.