I am trying to find the number of weeks or number of months between two dates. I have written the following but it does not work for the case when we have 53 weeks. I am unable to find a proper way to do this.
getNumOfWeeksOrMonths(fromDate:Moment, toDate:Moment, frequency:string){
let numberWeeksMonths;
if(frequency === 'months'){
console.log("Returning months");
numberWeeksMonths =moment(toDate).diff(moment(fromDate), 'months');
}
else{
console.log("Returning weeks");
numberWeeksMonths =moment(toDate).diff(moment(fromDate), 'weeks');
}
return numberWeeksMonths;
}
UPDATE: I am able to get the weeks as follows
let startDate = fromDate.toDate();
let endDate = toDate.toDate();
var week = 1000 * 60 * 60 * 24 * 7;
let weeksBetween = Math.ceil((endDate.getTime() - startDate.getTime())/ONE_WEEK)
This gives me correctly 52 weeks in 2019 and 53 weeks in 2020. However when I do a date range say 10/19/2020 to 11/30/2020 I get 7 weeks instead of 6. Any idea on how to fix this?