Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have to calculate number of days between two dates but every month is of 30
30 x 12 = 360 ,

What I have tried:

function diff_year_month_day(dt1, dt2) 
 {
 
  var time =(dt2.getTime() - dt1.getTime()) / 1000;
  var year  = Math.abs(Math.round((time/(60 * 60 * 24))/365.25));
  var month = Math.abs(Math.round(time/(60 * 60 * 24 * 7 * 4)));
  var days = Math.abs(Math.round(time/(3600 * 24)));
  return "Year :- " + year + " Month :- " + month + " Days :-" + days;
   
 }
Posted
Updated 17-Mar-22 2:15am
Comments
Richard MacCutchan 17-Mar-22 8:14am    
Not all months have 30 days.
Richard Deeming 18-Mar-22 5:19am    
Every month except February has 30 days; it's just that some of them have more. :)
Richard MacCutchan 18-Mar-22 5:26am    
One day I hope to learn how to speak and write proper English. :(

1 solution

Convert the date to a "day of year":
monthOfYear * 30 + dayOfMonth
Then include the year to make a "dayNumber" by multiplying the year by 360 and adding that.
Simple subtraction does the rest!
 
Share this answer
 
Comments
Maciej Los 18-Mar-22 11:16am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900