Skip to content

Instantly share code, notes, and snippets.

@enlacee
Created October 30, 2021 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enlacee/6b11fe2918433790124a24155b8950e1 to your computer and use it in GitHub Desktop.
Save enlacee/6b11fe2918433790124a24155b8950e1 to your computer and use it in GitHub Desktop.
GET NUMBER WEEKS WITH JS
function parseDate(str) {
var mdy = str.split('-');
return new Date(mdy[0], mdy[1]-1, mdy[2]);
}
function datediff(first, second) {
// Take the difference between the dates and divide by milliseconds per day.
// Round to nearest whole number to deal with DST.
return Math.round((second-first)/(1000*60*60*24));
}
let strDateFirt = '2021-01-01';
let strDateSecond = '2021-01-02';
let numberDays = datediff(
parseDate(strDateFirt),
parseDate(strDateSecond)
);
let numberWeeks = 0;
if (numberDays >= 7 ){
numberWeeks = Math.floor( (numberDays/7) );
}
console.log('numberWeeks', numberWeeks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment