Skip to content

Instantly share code, notes, and snippets.

@davidbarredo
Last active December 28, 2015 18:19
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 davidbarredo/7542370 to your computer and use it in GitHub Desktop.
Save davidbarredo/7542370 to your computer and use it in GitHub Desktop.
Util date extensions
// 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