# Code coach Days between dates (SOLVED)

Hello, I wrote this code but it works only for the first three test cases. I cannot figure out what is the problem. I would apreciate if could help me or at least give me an idea about the problem. Here is the problem: You need to calculate exactly how many days have passed between two dates. Task: Calculate how many days have passed between two input dates, and output the result. Input Format: Two strings that represent the dates, first date should be the older date. Date format: Month DD, YYYY Output Format: A number representing the number of days between the two dates. Sample Input: August 15, 1979 June 15, 2018 Sample Output: 14184 Here is the code: https://code.sololearn.com/c5MXU431EEmv/#c

10/19/2021 7:31:08 PM

Madalina Peter4 Answers

New AnswerMadalina Peter, in your previous question... https://www.sololearn.com/Discuss/2906552 I informed you that... "The C standard library provides functions for handling dates and times in <time.h>. In order to become productive in C it is necessary to study the C standard library." Just as in your previous question you can build a struct tm structure from the dates entered. These can then be converted into time_t values using the standard library function mktime(). The difference between these dates can then be computed using the standard library function difftime(). The difftime() function returns the difference between two time_t values in seconds. Simply divide this value by the number of seconds in a day to get the number of days. Another solution is to calculate the Julian date, or modified Julian date, for the two dates and then subtract the earlier date from the later date. https://en.wikipedia.org/wiki/Julian_day Edit: It took less than 5 minutes to convert the previous code into a solution for this problem. That includes compiling and testing.

Ok. Thank you. I will try that. I've tried a solution with time.h but it didn't work. I'll try this again with your advice.

Madalina Peter, your montoint() function is dangerous as it is accessing a char array out of bounds. Your character array is defined as... char mon[3]; but later in your code you write... mon[3] = '\0'; // <-- DANGER! Array access out of bounds. An array with 3 elements contains indexes from 0 to 2 inclusive. The element mon[3] does not exist. If you check my code for your previous problem you will notice that the char array is defined with 4 elements. This is because we require 3 elements for the month short name (JAN, FEB, MAR etc.) plus one more element to hold the null terminating character. Your montoint() function is returning the month number in the range 1 to 12. My original code returned the month number as 0 to 11 in order to make it compatible with the tm structure. If you were using your function to fill in a tm struct then don't forget to subtract 1 from the month number. Similarly do not forget to subtract 1900 from the year. Failure to fill in the tm struct correctly will result in incorrect date/time calculations.

I've succeded to do it with time.h Thank your very much for your help! Here is the code: https://code.sololearn.com/caxVrM7IhJAV