Getting the difference between dates

This is a discussion on Getting the difference between dates within the C Programming forums, part of the General Programming Boards category; Hello again. What I need to do this time is get a date from the user and then compare it ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    53

    Getting the difference between dates

    Hello again. What I need to do this time is get a date from the user and then compare it to the current date, to see if there are more than (int days) inbetween those two dates. I looked up a few bits in Google, but I only found a function using strptime which isn't in my time.h.

    Any ideas that would get me going? I have a procedure in my mind that could do it (convert both dates into days by month * 28, month * 29, month * 30, month * 31 respectively) but it's really, really lengthy and I want to avoid it.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    difftime is ANSI function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    53
    Code:
    scanf("%d/%d/%d", &soldiers.dateout.dd, &soldiers.dateout.mm, &soldiers.dateout.yyyy);
    Code:
    int comparedates(struct sdate dateout)
    {
    	time_t curtime;
    	struct tm tdateout;
    	
    	time(&curtime);
    	tdateout.tm_mday = dateout.dd;
    	tdateout.tm_mon = dateout.mm;
    	tdateout.tm_year = dateout.yyyy - 1900;
    	
    	if (difftime(tdateout, curtime) < 3888000) return -1;
    	else return 1;
    }
    Could this work to find if the difference between the current date and the entered date is smaller or not than 45 days?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    difftime takes two time_t objects, so you'll have to convert tdateout to a time_t using mktime(). It might not hurt to initialize some of the other parts of tdateout also (since we're comparing down to the second, either setting the time to midnight or the current time).

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    53
    Yeah, you're right on that last part, I shouldn't just have guessed that it initializes to 12:00am.

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Leftos View Post
    Hello again. What I need to do this time is get a date from the user and then compare it to the current date, to see if there are more than (int days) inbetween those two dates. I looked up a few bits in Google, but I only found a function using strptime which isn't in my time.h.

    Any ideas that would get me going? I have a procedure in my mind that could do it (convert both dates into days by month * 28, month * 29, month * 30, month * 31 respectively) but it's really, really lengthy and I want to avoid it.
    Hi!!
    I once wrote a program to do that!!
    I converted it from the type of really lengthy program you describe.
    I think I did it in about 5 lines in the end!!
    I would be interested to see if anyone else can do it in about the same number of lines.
    It too me a while to figure out how to do it but it defiinately can be done.
    I can't remember exactly how I did it at the moment (well not enough to write the program right now anyway).
    Basically you added 365 for each year and then 1 for each leap year then you just have to take care of the months which you assume are 30 then use an offset for a particular month, so you would have a table containing an offset for each month such as
    ( 1,-2,1,0,1,0,1,1,0,1,0,1). So you just did the difference in years X 365 then difference in months times 30 then, then you added your offsets which is dead easy for the months, bit harder for the leap years which are one on every 4 normally but there are differences if the dates are divisible by 400, or something like that.
    You also might get problems with various people, Popes, adding or taking away a few days here and there.
    Not sure how how difftime does it, but I expect my method is faster

    I will have a go at doing it again sometime and see if it is faster
    Bound to be, one would imagine
    Last edited by esbo; 01-17-2008 at 10:52 PM.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I think I did it in about 5 lines in the end!!
    I would be interested to see if anyone else can do it in about the same number of lines.
    Hey Esbo, would this qualify?

    Code:
    #include <stdio.h>
    
    unsigned long CalcDays(int iMonth, int iDay, int iYear)
    {
        return (iDay + (153 * (iMonth + 12 * ((14 - iMonth) / 12) - 3) + 2) / 5 + 365 *
            (iYear + 4800 - ((14 - iMonth) / 12)) + (iYear + 4800 - ((14 - iMonth) / 12)) / 4 - 32083);
    }
    
    int main(void)
    {
        printf("%d\n", CalcDays(1, 17, 2008) - CalcDays(1, 03, 2008) );
        return 0;
    }

  8. #8
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by BobS0327 View Post
    Hey Esbo, would this qualify?

    Code:
    #include <stdio.h>
    
    unsigned long CalcDays(int iMonth, int iDay, int iYear)
    {
        return (iDay + (153 * (iMonth + 12 * ((14 - iMonth) / 12) - 3) + 2) / 5 + 365 *
            (iYear + 4800 - ((14 - iMonth) / 12)) + (iYear + 4800 - ((14 - iMonth) / 12)) / 4 - 32083);
    }
    
    int main(void)
    {
        printf("%d\n", CalcDays(1, 17, 2008) - CalcDays(1, 03, 2008) );
        return 0;
    }
    Hi Bob!
    I am not sure yet as I can't understand it yet, will it work over a period of several years?
    Using the same year and month is not a very good test as that is the tricky bit.
    I will have a better look at it later and see if I can recreate my old program too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference between dates
    By DaniiChris in forum C Programming
    Replies: 11
    Last Post: 08-18-2008, 08:18 AM
  3. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  4. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  5. What is the Difference between ANSI C and non-ANSI C ?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-24-2001, 05:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21