We need to get week number OF MONTH in JS, for make a alert in HTML, any idea? We try using getday js function and yes, it work, but just show the number day of generic week.
Asked
Active
Viewed 6,791 times
0
-
3http://momentjs.com/ – Ivan Bacher Apr 01 '16 at 12:05
-
4http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php – messerbill Apr 01 '16 at 12:06
-
Try something, then show what you have tried in your question, along with errors, actual/expected behavior – Ruan Mendes Apr 01 '16 at 13:27
1 Answers
2
call function with : (new Date()).getWeek()
Date.prototype.getWeek = function() {
var date = new Date(this.getTime());
date.setHours(0, 0, 0, 0);
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
var week1 = new Date(date.getFullYear(), 0, 4);
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
- 3 + (week1.getDay() + 6) % 7) / 7);
}
Date.prototype.getWeekYear = function() {
var date = new Date(this.getTime());
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
return date.getFullYear();
}

Abdellah OUMGHAR
- 3,627
- 1
- 11
- 16
-
3
-
Doesn't seem to work, I `new Date(2000, 5, 26).getWeek()` returns 26, and according to http://www.epochconverter.com/weeks/2000, April, 26 2000 should be week 17 – Ruan Mendes Apr 01 '16 at 13:36
-
If you want to create a Date object using new Date (2000, 5, 26), start of the month index = 0 (5 = June), so you have to use new Date (2000, 4, 26). You can use the following syntax new Date ('5/26/2016') – Abdellah OUMGHAR Apr 01 '16 at 15:31
-