How to calculate the number of months between two dates

actually i want to do that day calculation based on two different date. one date come from data base. another one is get from real device how can i do that?

1 Like

kinda depends on how you want to determine a months

If you want to use built in.

Start is 2017-09-23T19:32:42.000Z
End is 2017-10-12T19:32:42.000Z

dateCompare(start: string,end: string) {

let startDt = new Date(start);
let endDt = new Date(end);

console.clear();
console.log(startDt);
console.log(endDt);
console.log(startDt.getFullYear()+'-'+startDt.getMonth()+'-'+startDt.getDay());
console.log(endDt.getFullYear()+'-'+endDt.getMonth()+'-'+endDt.getDay());
console.log(endDt.getMonth() - startDt.getMonth());  }

console →
Date 2017-09-23T19:32:42.000Z date-functions.ts:25:4
Date 2017-10-12T19:32:42.000Z date-functions.ts:27:3
2017-8-6 date-functions.ts:30
2017-9-4 date-functions.ts:30:1
1 < this is just simple month numbers

or are you wanting to count days between
you can look here for examples

date functions

To get the number of months between two dates:

Formula:

months = (year2 - year1) * 12 + (month2 - month1)
if day2 < day1: months -= 1

This gives the count of full months between the dates.