-
-
Save dblock/1081513 to your computer and use it in GitHub Desktop.
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; | |
} |
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 withnew Date(2016, 0, 1)
andnew Date(2016, 0, 4)
and you'll see that it jumps from 53 to 2I 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
Oh wow I just saw all these comments. Maybe someone can take this out in a JS library and write some tests?
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
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
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.
Working example for my case, taking straight from here: