Thread: calculate the days between two dates stored in strings

  1. #1
    Registered User
    Join Date
    May 2012

    Exclamation calculate the days between two dates stored in strings

    I have to strings with dates like '20120527' and '20120626'.

    Can anyone help? i need to calculate the days between them.

    thanks in advance.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    London, United Kingdom
    What have you done so far?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jan 2009
    In time.h there is a struct tm with fields for year, month, day of month and much more. Create a struct tm for each date, parse your strings and fill in the fields required to represent the dates.

    Then use difftime() to get the the time difference in seconds, just divide by seconds per day. difftime takes an epoch number not a struct tm though, so you need to convert them, you can do that with mktime().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculate the number of Days between two dates
    By wablackwell in forum C Programming
    Replies: 1
    Last Post: 04-02-2012, 10:02 PM
  2. Counting days between 2 dates
    By nynicue in forum C Programming
    Replies: 12
    Last Post: 02-19-2010, 10:50 AM
  3. number of days between 2 dates.
    By explosive in forum C++ Programming
    Replies: 10
    Last Post: 02-17-2005, 07:30 AM
  4. days between dates formula
    By maes in forum A Brief History of
    Replies: 4
    Last Post: 07-12-2004, 12:52 PM
  5. Calculating days between two Dates
    By Niy in forum C++ Programming
    Replies: 9
    Last Post: 03-12-2002, 08:16 PM