Skip to content

Instantly share code, notes, and snippets.

@AkarshSatija
Forked from dblock/getWeek.js
Last active August 29, 2015 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AkarshSatija/7df337780c94c5d885c6 to your computer and use it in GitHub Desktop.
Save AkarshSatija/7df337780c94c5d885c6 to your computer and use it in GitHub Desktop.
Get the week Number of the year
function( d ) {
// Create a copy of this date object
var target = new Date(d.valueOf());
// ISO week date weeks start on monday
// so correct the day number
var dayNr = (d.getDay() + 6) % 7;
// Set the target to the thursday of this week so the
// target date is in the right year
target.setDate(target.getDate() - dayNr + 3);
// ISO 8601 states that week 1 is the week
// with january 4th in it
var jan4 = new Date(target.getFullYear(), 0, 4);
// Number of days between target date and january 4th
var dayDiff = (target - jan4) / 86400000;
// Calculate week number: Week 1 (january 4th) plus the
// number of weeks between target date and january 4th
var weekNr = 1 + Math.ceil(dayDiff / 7);
return weekNr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment