0

I am reopening this thread with more information provided.

Having February 28 days, the result is 0.933... months, not the “1 month” I expected to get in these cases of months with 28/29/31 days (see attached screenshot:

)

How can I make the difference of 2 dates automatically (even if the month has 28, 30 or 31 days) and get the result "1" instead of 0.9333?

Below is the code im using:

var date1 = new Date("02/01/2022");
var date2 = new Date("03/01/2022");

// To calculate the time difference of two dates
var Difference_In_Time = date2.getTime() - date1.getTime();

// To calculate the no. of days between two dates
var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);
// To calculate the no. of months between two dates
var Difference_In_Months = Difference_In_Time / (1000 * 3600 * 24 * 30);
console.log(Difference_In_Time);
console.log(Difference_In_Days);
console.log(Difference_In_Months);

I want to make a calculation on a field which calculates the "Minimum Duration in Month".

In cases of full months (30),the calculation is easy (end date-start date), but in cases of 31 or 28 day months, the subtraction is not very accurate.

Many thanks!

RMMA

RMMA
  • 9
  • 1
  • The most important advice first: do not use w3schools. If you look at your code, you'll see that it always uses a factor of 30 for the days per month. So naturally, the results will be off. –  Feb 21 '22 at 11:51
  • You've to notify how many days there are in the months you're dealing with (`(1000 * 3600 * 24 * *here*)`). But, when you're calculating a time duration, the result 0.933 is correct. – Teemu Feb 21 '22 at 11:54
  • 2
    Ok, so the difference between March 1st and February 1st 2022 is 1 month. What is the expected difference between March 2nd and February 1st? 1 1/28? And between April 2nd and March 1st? 1 1/31? Please clarify how exact you want the result to be and why it matters. Or are you simply looking for "x months and y days"? –  Feb 21 '22 at 11:57
  • Welcome to Stack Overflow! Welcome to Stack Overflow! Please visit the [help], take the [tour] to see what and [ask]. If you get stuck, post a minimal reproducible example of your attempt, noting input and expected output using the `[<>]` [snippet editor](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do). But first: [find possible dupes](https://www.google.com/search?q=number+of+months+between+two+dates+site%3Astackoverflow.com) – mplungjan Feb 21 '22 at 12:07
  • Thanks for all the answers and I apologize for the "newbie" questions. – RMMA Feb 21 '22 at 12:51
  • @ChrisG I want to make a calculation on a field which calculates the "Minimum Duration in Month". In cases of full months,the calculation is easy (end date-start date), but in cases of 31 or 28 day months, the subtraction is not very accurate. Thanks in advance – RMMA Feb 21 '22 at 14:38
  • "Minimum Duration in Month" is supposed to be what exactly? Did you see my 2nd comment? I asked you a bunch of questions but you didn't answer them, just re-iterate your question. Please clarify exactly what the desired result is supposed to be with respect to the questions for clarification I asked you. –  Feb 21 '22 at 17:32
  • ... I just realized you literally used the exact same text from your question in the comment? Why would you do that? I asked you stuff because the question is unclear and cannot be solved when taken at face value. –  Feb 21 '22 at 18:43

1 Answers1

-1
const differenceInMonths = Math.abs(date1.getMonth() - date2.getMonth())

Seiteros
  • 191
  • 1
  • 2
  • 6