Skip to content

Instantly share code, notes, and snippets.

@motss
Last active February 1, 2016 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motss/627f95897fe4c926c158 to your computer and use it in GitHub Desktop.
Save motss/627f95897fe4c926c158 to your computer and use it in GitHub Desktop.
getWeek
// compute week number based on date and first day of the week is Sunday.
// Eg. Let _date = '2015-11-12',
// Ref 1 for week number starting from Monday: http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=2020
// Ref 2 for week number starting from Sunday: http://www.timeanddate.com/calendar/custom.html?year=2026&country=69&fdow=7&wno=4&df=1
// To get week numbers for calendars starting from Sunday (US calendar system).
function getWeek(_date) {
let now = new Date(_date); // new Date('2015-11-12');
now = new Date(now.getFullYear(), now.getMonth(), now.getDate() - now.getDay() + 4); // FIrst 4-day week of a year.
let onejan = new Date(now.getFullYear(), 0, 1);
// one day has 86400000 milliseconds.
return Math.ceil(((now - onejan) / 86400000 + 1) / 7);
}
// To get week numbers for calendars starting from Monday (ISO-8601).
function getWeekStartingFromMonday(_date) {
let now = new Date(_date);
// First 4-day week f a year. However, the week starts from 1 (Monday) to 7 (Sunday).
// If getDay() is 0, normalize it to 7.
now = new Date(now.getFullYear(), now.getMonth(), now.getDate() - (now.getDay() || 7) + 4);
let onejan = new Date(now.getFullYear(), 0, 1);
// one day has 86400000 milliseconds.
return Math.ceil(((now - onejan) / 86400000 + 1) / 7);
}
// Different result when using setDate instead of new Date(now.getFullYear(), now.getMonth(), now.getDate() - now.getDay() + 4).
/*
function getWeekUsingSetDate(_date) {
let now = new Date(_date);
now.setDate(now.getDate() - now.getDay() + 4); // setDate seemed to yield inaccurate week number.
let onejan = new Date(now.getFullYear(), 0, 1);
return Math.ceil(((now - onejan) / 86400000 + 1) / 7);
}
*/
for (let i = -10; i < 20; i++) {
console.log(`${2016 + i}-1-1`, getWeek(`${2016 + i}-1-31`), getWeekUsingSetDate(`${2016 + i}-1-31`));
}
/*
2006-1-1 1 1
2007-1-1 1 1
2008-1-1 1 1
2009-1-1 1 1
2010-1-1 53 53
2011-1-1 52 52
2012-1-1 1 1
2013-1-1 1 1
2014-1-1 1 1
2015-1-1 1 1
2016-1-1 53 53
2017-1-1 1 1
2018-1-1 1 1
2019-1-1 1 1
2020-1-1 1 1
2021-1-1 53 53
2022-1-1 52 52
2023-1-1 1 1
2024-1-1 1 1
2025-1-1 1 1
2026-1-1 1 1
2027-1-1 53 53
2028-1-1 52 52
2029-1-1 1 1
2030-1-1 1 1
2031-1-1 1 1
2032-1-1 1 1
2033-1-1 53 53
2034-1-1 1 1
2035-1-1 1 1
*/
for (let i = -10; i < 20; i++) {
console.log(`${2016 + i}-12-31`, getWeek(`${2016 + i}-12-31`), getWeekUsingSetDate(`${2016 + i}-12-31`));
}
/*
2006-12-31 1 1
2007-12-31 1 1
2008-12-31 1 1
2009-12-31 53 53
2010-12-31 52 53 *52 is the correct week number.
2011-12-31 52 52
2012-12-31 1 1
2013-12-31 1 1
2014-12-31 1 1
2015-12-31 53 53
2016-12-31 52 53 *52 is the correct week number.
2017-12-31 1 1
2018-12-31 1 1
2019-12-31 1 1
2020-12-31 53 53
2021-12-31 52 53 *52 is the correct week number.
2022-12-31 52 52
2023-12-31 1 1
2024-12-31 1 1
2025-12-31 1 1
2026-12-31 53 53
2027-12-31 52 53 *52 is the correct week number.
2028-12-31 1 1
2029-12-31 1 1
2030-12-31 1 1
2031-12-31 1 1
2032-12-31 53 53
2033-12-31 52 52
2034-12-31 1 1
2035-12-31 1 1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment