How many weeks in a year

Given a specific year, how can I calculate, in javascript/JQuery, how many weeks are in the given year? 52 or 53?

I've looked in many answers but none worked for me yet- I need to calculate the first day of year as the first week (as displayed in Outlook calendar). In the calculations that I saw, the first week starts from the first Sunday in the year.

outlook calendar - 12/2016

4

1 Answer

If it's the ISO 8601 week number you are looking for you can do this by checking which week number december 28 has as that date is always in the last week of the year like january 4 is always in the first week:

 Date.prototype.getWeek = function() { var date = new Date(this.getTime()); date.setHours(0, 0, 0, 0); // Thursday in current week decides the year. date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7); // January 4 is always in week 1. var week1 = new Date(date.getFullYear(), 0, 4); // Adjust to Thursday in week 1 and count number of weeks from date to week1. return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7); } console.log((new Date(2019, 11, 28)).getWeek()); // 52 console.log((new Date(2020, 11, 28)).getWeek()); // 53 console.log((new Date(2021, 11, 28)).getWeek()); // 52
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like