Skip to content

Instantly share code, notes, and snippets.

@dblock
Created July 13, 2011 22:49
Show Gist options
  • Save dblock/1081513 to your computer and use it in GitHub Desktop.
Save dblock/1081513 to your computer and use it in GitHub Desktop.
get week of the year in JavaScript
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;
}
@the-eater
Copy link

How do I get the week number of a week starting from sunday?

@brickpop
Copy link

There is an error in this code.
If Jan 4th falls on Fri/Sat/Sun, the first week of the year is "2", according to this code.
Try with new Date(2016, 0, 1) and new Date(2016, 0, 4) and you'll see that it jumps from 53 to 2

I have added an if statement in the end that fixes this scenario.

Date.prototype.getWeek = function() { 

  // Create a copy of this date object  
  var target  = new Date(this.valueOf());  

  // ISO week date weeks start on monday, so correct the day number  
  var dayNr   = (this.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;    

  if(new Date(target.getFullYear(), 0, 1).getDay() < 5) {
    // Calculate week number: Week 1 (january 4th) plus the    
    // number of weeks between target date and january 4th    
    return 1 + Math.ceil(dayDiff / 7);    
  }
  else {  // jan 4th is on the next week (so next week is week 1)
    return Math.ceil(dayDiff / 7); 
  }
}; 

@gillminister
Copy link

Heads up @ledfusion:

Your code contains an error too. Check with 1. Jan 2017 vs 2. Jan 2017. The latter returns week 2.

I'll post if I find a solution.

Edit:
The issue has been addressed at stackoverflow:
http://stackoverflow.com/a/6117889/6025684

var weekOfYear = function(date){
    var d = new Date(+date);
    d.setHours(0,0,0);
    d.setDate(d.getDate()+4-(d.getDay()||7));
    return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7);
};

I have only performed lightweight testing; use with caution.

Tested for range:
31. Dec - 5. Jan, years 1994 (Dec) - 2019 (Jan).

@sils
Copy link

sils commented Jul 10, 2016

Hi, what's the license? Can I reuse that code?

@robeverett
Copy link

How could this code be adapted to include the following requirements:

  1. Our fiscal year begins on 1st April each year.

  2. Our working week begins on a Friday at 17:00 hours (don't ask lol)

  3. I simply want to return the week number for any year from 1st April 2016 onwards given any date of the year.

Would be very much appreciated.

@knm1993
Copy link

knm1993 commented Apr 16, 2018

Hello, there are always an exception: 31 Dec of any year is being returned wrongly as the first week of the year. It should be 52.

@necipallef
Copy link

Working example for my case, taking straight from here:

/* For a given date, get the ISO week number
 *
 * Based on information at:
 *
 *    http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
 *
 * Algorithm is to find nearest thursday, it's year
 * is the year of the week number. Then get weeks
 * between that date and the first day of that year.
 *
 * Note that dates in one year can be weeks of previous
 * or next year, overlap is up to 3 days.
 *
 * e.g. 2014/12/29 is Monday in week  1 of 2015
 *      2012/1/1   is Sunday in week 52 of 2011
 */
function getWeekNumber(d) {
    // Copy date so don't modify original
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
    // Set to nearest Thursday: current date + 4 - current day number
    // Make Sunday's day number 7
    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
    // Get first day of year
    var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
    // Calculate full weeks to nearest Thursday
    var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
    // Return array of year and week number
    return [d.getUTCFullYear(), weekNo];
}

@ngnam
Copy link

ngnam commented Apr 3, 2020

There is an error in this code.
If Jan 4th falls on Fri/Sat/Sun, the first week of the year is "2", according to this code.
Try with new Date(2016, 0, 1) and new Date(2016, 0, 4) and you'll see that it jumps from 53 to 2

I have added an if statement in the end that fixes this scenario.

Date.prototype.getWeek = function() { 

  // Create a copy of this date object  
  var target  = new Date(this.valueOf());  

  // ISO week date weeks start on monday, so correct the day number  
  var dayNr   = (this.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;    

  if(new Date(target.getFullYear(), 0, 1).getDay() < 5) {
    // Calculate week number: Week 1 (january 4th) plus the    
    // number of weeks between target date and january 4th    
    return 1 + Math.ceil(dayDiff / 7);    
  }
  else {  // jan 4th is on the next week (so next week is week 1)
    return Math.ceil(dayDiff / 7); 
  }
}; 

thanks you

@dblock
Copy link
Author

dblock commented Apr 3, 2020

Oh wow I just saw all these comments. Maybe someone can take this out in a JS library and write some tests?

@ngnam
Copy link

ngnam commented Apr 3, 2020

Oh wow I just saw all these comments. Maybe someone can take this out in a JS library and write some tests?

yes, I'll try it

@FSYED7X
Copy link

FSYED7X commented Aug 30, 2020

I have modified the code a bit.
What I want to achieve is - I want to get 2 weeks at a time. So I have made 14 instead of all 7's.
This is working fine but the problem causing is - The starting day of the week is now set to Sunday.
How can I make it back to Monday???

`Date.prototype.get2Weeks = function() {

// Create a copy of this date object  
var target  = new Date(this.valueOf());  

// ISO week date weeks start on monday, so correct the day number  
var dayNr   = (this.getDay() + 6) % 14;  

// Set the target to the thursday of this week so the  
console.log(dayNr)
// 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;    

if(new Date(target.getFullYear(), 0, 1).getDay() < 5) {
  // Calculate week number: Week 1 (january 4th) plus the    
  // number of weeks between target date and january 4th    
  return 2 + Math.ceil(dayDiff / 14);    
}
else {  // jan 4th is on the next week (so next week is week 1)
  return Math.ceil(dayDiff / 14); 
}

}; `

Thank you

@dblock
Copy link
Author

dblock commented Sep 15, 2020

Anyone tried https://github.com/jsdocker/week-utils? Another library that includes this functionality? If nothing works I'll contribute this somewhere or make a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment