Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save irkreja/833ceaf9cf309bfa28545a802a076279 to your computer and use it in GitHub Desktop.
Save irkreja/833ceaf9cf309bfa28545a802a076279 to your computer and use it in GitHub Desktop.
function ISO8601_week_no(dt)
{
var tdt = new Date(dt.valueOf());
var dayn = (dt.getDay() + 6) % 7;
tdt.setDate(tdt.getDate() - dayn + 3);
var firstThursday = tdt.valueOf();
tdt.setMonth(0, 1);
if (tdt.getDay() !== 4)
{
tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7);
}
return 1 + Math.ceil((firstThursday - tdt) / 604800000);
}
dt = new Date();
console.log(ISO8601_week_no(dt));
dt = new Date(2015, 10, 1);
console.log(ISO8601_week_no(dt));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment