Nodejs Utililty Methods Week Calculate

List of utility methods to do Week Calculate

Description

The list of methods to do Week Calculate are organized into topic(s).

Method

toWeekDays()
Date.prototype.toWeekDays = function () {
  var weekday = this.clone()
  if (!weekday.is().sunday()) {
    weekday.sunday().add(-7).days()
  var week = [weekday.clone()]
  for (var day = 0; day < 6; day++) {
    week.push(weekday.add(1).days().clone())
  return week
Date.prototype.toNextWeekDays = function () {
  return this.add(7).days().toWeekDays()
Date.prototype.toPreviousWeekDays = function () {
  return this.add(-7).days().toWeekDays()
Date.prototype.compare = function (date) {
  var year = this.getYear() - date.getYear()
  if (year > 0)
    return "In " + year + " Years"
  else if (year < 0)
    return -year + " Years Ago"
  else {
    var month = this.getMonth() - date.getMonth()
    if (month > 0)
      return "In " + month + " Months"
    else if (month < 0)
      return -month + " Months Ago"
    else {
      var week = this.getWeekOfYear() - date.getWeekOfYear()
      if (week > 0)
        return "In " + week + " Weeks"
      else if (week < 0)
        return -week + " Weeks Ago"
      else {
        if (this.getDay() - date.getDay() == 0)
          return "Today"
        else
          return this.toString("dddd")
toWeekEnd(startsFromSunday)
Date.prototype.toWeekEnd = function (startsFromSunday) {
    var dayOfWeek = this.getDay();
    if (startsFromSunday) {
        return this.addDays(6 - dayOfWeek);
    if (dayOfWeek == 0) {
        dayOfWeek = 7;
    return this.addDays(7 - dayOfWeek);
...
toWeekStart(startsFromSunday)
Date.prototype.toWeekStart = function (startsFromSunday) {
    var dayOfWeek = this.getDay();
    if (startsFromSunday) {
        return this.addDays(-dayOfWeek);
    if (dayOfWeek == 0) {
        dayOfWeek = 7;
    return this.addDays(1 - dayOfWeek);
...
weekNo()
var firstMonday = new Date(1970, 0, 5).getTime()
Date.prototype.weekNo = function() {
  var _date = new Date(this)
  _date.setHours(0,0,0,0)
  _date.setDate(_date.getDate() - (_date.getDay() || 7))
  var weekNo = Math.ceil((((_date - firstMonday) / 86400000) + 1) / 7)
  return weekNo
Date.prototype.weekNoRelative = function() {
...
weekNumber()
Date.prototype.weekNumber = function() {
   var MaDate = this;
   var mm = MaDate.getMonth(), jj = MaDate.getDate();
   var annee = MaDate.getFullYear();
   var NumSemaine = 0,
   ListeMois = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
   if (annee %4 == 0 && annee %100 !=0 || annee %400 == 0) {ListeMois[1]=29};
   var TotalJour=0;
   for(cpt=0; cpt<mm; cpt++){TotalJour+=ListeMois[cpt];}
...
weekday()
Date.prototype.weekday = function()
  var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  return days[this.getDay()];