How to calculate number of weaks between two dates in javascript?

How could I get the difference for two Date() objects in JavaScript, while only return the number of weaks in the difference?.

I struggle a lot to find the difference between two dates and finally a lots of googling i find the below solution :


var date = new Date("2017/04/04"); var year = date.getFullYear(); var month = (date.getMonth()+1 <10) ? "0"+(parseInt(date.getMonth())+1) : parseInt(date.getMonth())+1; var day = (date.getDate()<10) ? "0"+date.getDate() : date.getDate(); var newdate = year+"/"+month+"/"+day; var date1 = new Date(newdate).getTime(); var date2 = new Date().getTime(); var dif = Math.floor(date2-date1); dif = Math.floor(dif/1000/60/60/24/7);
alert("Total number of weaks between date1 and date2 ="+dif);


You can check the above code. You will return the difference between the "2017/04/04" and Current date in weaks.



Output :




If you like this post don't forgot to leave a comment..

Chears :) Happy Coding..

Post a Comment

Previous Post Next Post