简体   繁体   中英

Find out week number of month from Date

How to Find out week number of the month from Date ...

(Date/7) and ceil/floor(Date/7) is not working for month Dec-2019 ( or any month which have 1 day is Sunday )...

My code:

var day = new Date($("#convDate").val()).getDay();
var week = 0 | new Date($("#convDate").val()).getDate() / 7;

week = Math.ceil(week);

if (week == 1 || week == 3) {
  if (day == 6) {
    alert("Half Day");
  }
}

alert("Submit");
return false;
Date.prototype.getMDay = function() {
  return (this.getDay() + 6) %7;
}

Date.prototype.getISOYear = function() {
  var thu = new Date(this.getFullYear(), this.getMonth(), this.getDate()+3-this.getMDay());
  return thu.getFullYear();
}

Date.prototype.getISOWeek = function() {
  var onejan = new Date(this.getISOYear(),0,1);
  var wk = Math.ceil((((this - onejan) / 86400000) + onejan.getMDay()+1)/7);
  if (onejan.getMDay() > 3) wk--;return wk;
}

week = (new Date('Dec 2019')).getISOWeek();  //48

I Found Solution Just Right Now, From Get week number of the month from date (weeks starting on Mondays)

Answered By Avraham [ Thanks a lot.. ]

function getWeek(date) {
  let monthStart = new Date(date);
  monthStart.setDate(0);
  let offset = (monthStart.getDay() + 1) % 7 - 1; // -1 is for a week starting on Monday
  return Math.ceil((date.getDate() + offset) / 7);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM