Created
October 30, 2021 03:33
-
-
Save enlacee/6b11fe2918433790124a24155b8950e1 to your computer and use it in GitHub Desktop.
GET NUMBER WEEKS WITH JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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