Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the number of days between two given dates? (Leap year obstacle)

Tags:

c

algorithm

  • Any year evenly divisible by 400 is a leap year (e.g., 2000 was a leap year).
  • Any other year evenly divisible by 100 is not a leap year (e.g., 1700, 1800 and 1900 were not leap years).
  • Any other year evenly divisible by 4 is a leap year (e.g., 1996 and 2004 are leap years).

But I'm not sure how to make nested if states in my c-program that would produce the right answer...

like image 888
user133466 Avatar asked Dec 01 '22 07:12

user133466


1 Answers

Convert them both to UNIX epoch time and subtract the difference.

UNIX Epoch time is the total number of seconds for a date since 1 January 1970 00:00:00.0

Once you've got the number of seconds, you divide that difference by the number of seconds in a day (which is 24 hours * 60 minutes * 60 seconds = 86400 seconds).

like image 123
Rap Avatar answered Dec 04 '22 07:12

Rap