Published
Edited
Aug 24, 2021
Insert cell
Insert cell
Insert cell
week = epiWeekOfDate(...date.split('-').map(d=>+d))
Insert cell
md`${date} is in epidemiological week #${week[1]} of ${week[0]}`
Insert cell
Inputs.table((weeks).map((d,i) => ({week: i+1, sunday: d.join("-"), saturday: adjustDate(d[0],d[1],d[2]+6).join("-")})));
Insert cell
weeks = epiWeeksOfYear(year)
Insert cell
function epiWeeksOfYear(y) {
let result = [];
let i = 1;
let firstDay = epiWeekFirstDay(y,i++);
while(firstDay[0] == y) {
result.push(firstDay);
firstDay = epiWeekFirstDay(y,i++);
}
return result;
}

Insert cell
function epiWeekFirstDay(y,weekIndex) {
let d0 = 1+(7-dow(y,1,1))%7;
return adjustDate(y,1,d0+7*(weekIndex-1));
}
Insert cell
function dow(y,m,d) {
// https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html
if(m<=2) {
m+=10;
y--;
} else {
m-=2;
}
const C = Math.floor(y/100);
y = y%100;
return (d+Math.floor((26*m-2)/10)-2*C+y+Math.floor(y/4)+Math.floor(C/4))%7;
}

Insert cell
function adjustDate(y,m,d) {
const ds = [31,((y%4==0)&&((y%100!=0)||(y%400==0))?29:28),31,30,31,30,31,31,30,31,30,31];
while(d > ds[m-1]) {
d = d-ds[m-1];
m++;
if(m>12) {
y++;
m=1;
}
}
return [y,m,d];
}
Insert cell
function epiWeekOfDate(y, m, d) {
const ds = [31,((y%4==0)&&((y%100!=0)||(y%400==0))?29:28),31,30,31,30,31,31,30,31,30,31];
for(let i=m-1; i>=1; i--) {
d+=ds[i-1];
}
let result = Math.floor((d - epiWeekFirstDay(y,1)[2])/7)+1;
if(result === 0) {
return epiWeekOfDate(y-1,12,31);
}
return [y,result];
}
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more