Last active
December 28, 2015 18:19
-
-
Save davidbarredo/7542370 to your computer and use it in GitHub Desktop.
Util date extensions
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
// Remember to prototype your own custom Date | |
var customDate = Date | |
// Date extension to to make monday day 0 (week start on monday) | |
customDate.prototype.getDayL = function () { | |
if ( this.getDay() == 0 ) { | |
return 6; | |
}else{ | |
return this.getDay() - 1; | |
} | |
} | |
// Date extension to get number of week in month | |
customDate.prototype.getWeekMonth = function (){ | |
var first = new Date( this.getFullYear(), this.getMonth(), 1 ); | |
return Math.ceil( ( this.getDate() + first.getDayL() ) / 7 ); | |
} | |
// Date extension to get number of week in year | |
customDate.prototype.getWeekYear = function (){ | |
var first = new Date( this.getFullYear(), 0, 1 ); | |
return Math.ceil( ( this.getDayYear() + first.getDay() ) / 7 ); | |
/* if weeks starts on monday using getDayL extension */ | |
// return Math.ceil( ( this.getDayYear() + first.getDayL() ) / 7 ); | |
} | |
// Date extension to get number of day in year | |
customDate.prototype.getDayYear = function (){ | |
var first = new Date( this.getFullYear(), 0, 1 ); | |
var diff = this - first; | |
var oneDay = 1000 * 60 * 60 * 24; | |
return diff / oneDay; | |
} | |
// Date extension to get number of days in month | |
customDate.prototype.getDaysInMonth = function (month, year){ | |
return new Date(year, month, 0).getDate(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment