Reputation: 389
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;
Upvotes: 1
Views: 1835
Reputation: 389
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);
}
Upvotes: 1
Reputation: 7683
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
Upvotes: 1