Get week of the month
How can i get the week number of month using javascript / jquery?
F开发者_如何学编程or ex.:
First Week: 5th July, 2010. / Week Number = First monday
Previous Week: 12th July, 2010. / Week Number = Second monday
Current Date: 19th July, 2010. / Week Number = Third Monday
Next week: 26th July, 2010. / Week Number = Last monday
This is an old question, here is my cross-browser solution based on:
- Weeks start on Sunday
- The first week of a month is the one that contains the first of the month
So in March 2013:
- Fri 1 Mar is the first day of week 1
- Sun 3 Mar is the start of week 2
- Sun 31 Mar is the start of week 6 (and is the only day in the that week)
- Mon 1 Apr is the first day of week 1 in April.
Date.prototype.getWeekOfMonth = function(exact) {
var month = this.getMonth()
, year = this.getFullYear()
, firstWeekday = new Date(year, month, 1).getDay()
, lastDateOfMonth = new Date(year, month + 1, 0).getDate()
, offsetDate = this.getDate() + firstWeekday - 1
, index = 1 // start index at 0 or 1, your choice
, weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7)
, week = index + Math.floor(offsetDate / 7)
;
if (exact || week < 2 + index) return week;
return week === weeksInMonth ? index + 5 : week;
};
// Simple helper to parse YYYY-MM-DD as local
function parseISOAsLocal(s){
var b = s.split(/\D/);
return new Date(b[0],b[1]-1,b[2]);
}
// Tests
console.log('Date Exact|expected not exact|expected');
[ ['2013-02-01', 1, 1],['2013-02-05', 2, 2],['2013-02-14', 3, 3],
['2013-02-23', 4, 4],['2013-02-24', 5, 6],['2013-02-28', 5, 6],
['2013-03-01', 1, 1],['2013-03-02', 1, 1],['2013-03-03', 2, 2],
['2013-03-15', 3, 3],['2013-03-17', 4, 4],['2013-03-23', 4, 4],
['2013-03-24', 5, 5],['2013-03-30', 5, 5],['2013-03-31', 6, 6],
['2013-04-01', 1, 1]
].forEach(function(test){
var d = parseISOAsLocal(test[0])
console.log(test[0] + ' ' +
d.getWeekOfMonth(true) + '|' + test[1] + ' ' +
d.getWeekOfMonth() + '|' + test[2]);
});
You don't need to put it directly on the prototype if you don't want to. In my implementation, 6 means "Last", not "Sixth". If you want it to always return the actual week of the month, just pass true
.
EDIT: Fixed this to handle 5 & 6-week months. My "unit tests", feel free to fork: http://jsfiddle.net/OlsonDev/5mXF6/1/.
Having struggle with this topic too - thanks to Olson.dev! I've shortened his function a little bit, if somebody is interessted:
// returns week of the month starting with 0
Date.prototype.getWeekOfMonth = function() {
var firstWeekday = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
var offsetDate = this.getDate() + firstWeekday - 1;
return Math.floor(offsetDate / 7);
}
update: if you need a localized version - you have to tweak the firstWeekday variable
// german version - week starts with monday
Date.prototype.getWeekOfMonth = function() {
var firstWeekday = new Date(this.getFullYear(), this.getMonth(), 1).getDay() - 1;
if (firstWeekday < 0) firstWeekday = 6;
var offsetDate = this.getDate() + firstWeekday - 1;
return Math.floor(offsetDate / 7);
}
I think this works. It returns the week of the month, starting at 0:
var d = new Date();
var date = d.getDate();
var day = d.getDay();
var weekOfMonth = Math.ceil((date - 1 - day) / 7);
function getWeekOfMonth(date) {
const startWeekDayIndex = 1; // 1 MonthDay 0 Sundays
const firstDate = new Date(date.getFullYear(), date.getMonth(), 1);
const firstDay = firstDate.getDay();
let weekNumber = Math.ceil((date.getDate() + firstDay) / 7);
if (startWeekDayIndex === 1) {
if (date.getDay() === 0 && date.getDate() > 1) {
weekNumber -= 1;
}
if (firstDate.getDate() === 1 && firstDay === 0 && date.getDate() > 1) {
weekNumber += 1;
}
}
return weekNumber;
}
I hope this works Tested until 2025
This is a few years on, but I've needed to use this functionality recently and, for certain dates in years 2016/2020 (such as January 31st), none of the code here works.
It's not the most efficient by any means, but hopefully this helps someone out as it's the only thing I could get working for those years along with every other year.
Date.prototype.getWeekOfMonth = function () {
var dayOfMonth = this.getDay();
var month = this.getMonth();
var year = this.getFullYear();
var checkDate = new Date(year, month, this.getDate());
var checkDateTime = checkDate.getTime();
var currentWeek = 0;
for (var i = 1; i < 32; i++) {
var loopDate = new Date(year, month, i);
if (loopDate.getDay() == dayOfMonth) {
currentWeek++;
}
if (loopDate.getTime() == checkDateTime) {
return currentWeek;
}
}
};
I think you want to use weekOfMonth
so it will give 1-4 or 1-5 week of month. I solved the same problem with this:
var dated = new Date();
var weekOfMonth = (0 | dated.getDate() / 7)+1;
week_number = 0 | new Date().getDate() / 7
import getWeekOfMonth from 'date-fns/getWeekOfMonth'
...
let weekOfMonth = getWeekOfMonth(new Date())
https://date-fns.org/v2.0.0-alpha.9/docs/getWeekOfMonth
None of the previous solutions included the "Last" on the response and I was able to do it by checking the next occurrence of the same day of the week and checking if it's still on this month.
The accepted answer fails in many cases (I tested it with today's date - 2021-05-14 - and it returned "Third Friday" when it's actually the second).
This code below was tested even with April 2017 (a rare case of a month with 6 weeks).
/**
* Get the week number of the month, from "First" to "Last"
* @param {Date} date
* @returns {string}
*/
function weekOfTheMonth(date) {
const day = date.getDate()
const weekDay = date.getDay()
let week = Math.ceil(day / 7)
const ordinal = ['First', 'Second', 'Third', 'Fourth', 'Last']
const weekDays = ['Sunday','Monday','Tuesday','Wednesday', 'Thursday','Friday','Saturday']
// Check the next day of the week and if it' on the same month, if not, respond with "Last"
const nextWeekDay = new Date(date.getTime() + (1000 * 60 * 60 * 24 * 7))
if (nextWeekDay.getMonth() !== date.getMonth()) {
week = 5
}
return `${ordinal[week - 1]} ${weekDays[weekDay]}`
}
const days = [
new Date('2021-05-14'),
new Date('26 July 2010'),
new Date('5 July 2010'),
new Date('12 July 2010'),
new Date('22 April 2017'),
new Date('29 April 2017'),
]
for (let i = 0; i < days.length; i += 1) {
const d = days[i]
console.log(d, weekOfTheMonth(d))
}
This is nothing natively supported.
You could roll your own function for this, working from the first day of the month
var currentDate = new Date();
var firstDayOfMonth = new Date( currentDate.getFullYear(), currentDate.getMonth(), 1 );
And then getting the weekday of that date:
var firstWeekday = firstDayOfMonth.getDay();
... which will give you a zero-based index, from 0-6, where 0 is Sunday.
I was just able to figure out a easier code for calculate the number of weeks for a given month of an year ..
y == year for example { 2012 } m == is a value from { 0 - 11 }
function weeks_Of_Month( y, m ) {
var first = new Date(y, m,1).getDay();
var last = 32 - new Date(y, m, 32).getDate();
// logic to calculate number of weeks for the current month
return Math.ceil( (first + last)/7 );
}
function weekNumberForDate(date){
var janOne = new Date(date.getFullYear(),0,1);
var _date = new Date(date.getFullYear(),date.getMonth(),date.getDate());
var yearDay = ((_date - janOne + 1) / 86400000);//60 * 60 * 24 * 1000
var day = janOne.getUTCDay();
if (day<4){yearDay+=day;}
var week = Math.ceil(yearDay/7);
return week;
}
Apparently the first week of the year is the week that contains that year's first Thursday.
Without calculating the UTCDay, the returned week was one week shy of what it should have been. Not confident this can't be improved, but seems to work for now.
function getWeekOfMonth(date) {
var nth = 0; // returning variable.
var timestamp = date.getTime(); // get UTC timestamp of date.
var month = date.getMonth(); // get current month.
var m = month; // save temp value of month.
while( m == month ) { // check if m equals our date's month.
nth++; // increment our week count.
// update m to reflect previous week (previous to last value of m).
m = new Date(timestamp - nth * 604800000).getMonth();
}
return nth;
}
After reading all the answers I figured out a way that use less CPU than the others and work for every day of every month of every year. Here is my code:
function getWeekInMonth(year, month, day){
let weekNum = 1; // we start at week 1
let weekDay = new Date(year, month - 1, 1).getDay(); // we get the weekDay of day 1
weekDay = weekDay === 0 ? 6 : weekDay-1; // we recalculate the weekDay (Mon:0, Tue:1, Wed:2, Thu:3, Fri:4, Sat:5, Sun:6)
let monday = 1+(7-weekDay); // we get the first monday of the month
while(monday <= day) { //we calculate in wich week is our day
weekNum++;
monday += 7;
}
return weekNum; //we return it
}
I hope this can help.
Please try below function.This one is considering week start date as Monday and week end date as Sunday.
getWeekNumber(date) {
var monthStartDate =new Date(new Date().getFullYear(), new
Date().getMonth(), 1);
monthStartDate = new Date(monthStartDate);
var day = startdate.getDay();
date = new Date(date);
var date = date.getDate();
return Math.ceil((date+ day-1)/ 7);
}
You just need this:
Math.ceil(new Date().getDate() / 7)
function weekAndDay(date) {
var days = ['Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday'],
prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];
return prefixes[Math.floor(date.getDate() / 7)] + ' ' + days[date.getDay()];
}
console.log( weekAndDay(new Date(2010,7-1, 5)) ); // => "First Monday"
console.log( weekAndDay(new Date(2010,7-1,12)) ); // => "Second Monday"
console.log( weekAndDay(new Date(2010,7-1,19)) ); // => "Third Monday"
console.log( weekAndDay(new Date(2010,7-1,26)) ); // => "Fourth Monday"
console.log( weekAndDay(new Date()) );
Adding the capability to have Last ...
may take some more hacking...
精彩评论