Skip to content

Instantly share code, notes, and snippets.

@nothinking
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save nothinking/ef2d6532df9a61030ee7 to your computer and use it in GitHub Desktop.

Select an option

Save nothinking/ef2d6532df9a61030ee7 to your computer and use it in GitHub Desktop.
moment-weekofmonth.js
moment.fn.weekOfMonthFormat = function(format) {
var formats = format.split("-");
return moment(formats[0] + "-" + formats[1], "YYYY-MM").weekOfMonth(formats[2]);
}
moment.fn.koDay = function() {
var day = this.clone().day() || 7;
return --day;
}
moment.fn.weekOfMonth = function(input) {
var day = this.clone().date(1).koDay();
if (input != null) {
var endOfWeek = input*7 - day;
var endOfMonth = this.clone().endOf('month').date();
endOfWeek = Math.min(endOfWeek, endOfMonth);
return endOfWeek > 0 ? this.date(endOfWeek) : this.add(-1, 'month').endOf('month');
} else {
return Math.ceil((this.date() + day) / 7);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment