Calculating days between two Dates

This is a discussion on Calculating days between two Dates within the C++ Programming forums, part of the General Programming Boards category; Anyone one of you can think of a clever algorithm? The date input format should be ( dd, mm, yyyy ...

  1. #1
    Niy
    Guest

    Question Calculating days between two Dates

    Anyone one of you can think of a clever algorithm?

    The date input format should be ( dd, mm, yyyy ). All I can think of is that to work out the number of months between the two date. And then work out the number of days for each month, And lastly add up all the days. But this is a pain in the s.

    Since leap year is taken into concern, the calculation is quite tedious (although not impossible). I am thinking of the possiblity of a shorter algorithm. It nice if you guys have some suggestions.

    Thanks a lot.

  2. #2
    Niy
    Guest
    No one knows a better method?

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Please don't bump your threads, doing so is against the rules and may result in your thread being deleted. (...and doing so so soon after your first post is pretty rude!!!).

    http://www.cprogramming.com/cboard/a...p?s=&forumid=3
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Unregistered
    Guest
    sounds like a reasonable method for working it out, not too hard either

  5. #5
    Yin
    Guest

    Unhappy

    Originally posted by adrianxw
    Please don't bump your threads, doing so is against the rules and may result in your thread being deleted. (...and doing so so soon after your first post is pretty rude!!!).

    http://www.cprogramming.com/cboard/a...p?s=&forumid=3
    oh, sorry... So do you have any idea about calculation in mind? I am now thinking whether the famous "Zeller's algorithm" will help. But seems unrelated...

  6. #6
    Niy
    Guest
    Originally posted by Yin


    oh, sorry... So do you have any idea about calculation in mind? I am now thinking whether the famous "Zeller's algorithm" will help. But seems unrelated...
    well, It should be me who say sorry. zeller is for calculating the day of a date (ie. sunday, monday, etc) isn't it? How can it help??

  7. #7
    Unregistered
    Guest
    Look into the time header file. Within there you will find several time funtions including difftime(). difftime() will give you the number of seconds between two different dates/time returned as type long. This can be directly translated into the number of days between two dates (divide by the product of 24 * 60 * 60. You can then divide 365 to give an approximation to the number of years. And then the remainder by 30 to get an approximation to the number of months with the remainder of that being the number of days. If you need EXACT number of months and years, then you will need to dig further in time header file than I have dug, or create the function yourself. One of the drawbacks to the time.h file is many of the functions use a struct called tm to hold data, or use type time_t or pointer to type time_t which is a bit of a nuisance until you get used to it.

  8. #8
    Niy
    Guest
    Originally posted by Unregistered
    Look into the time header file. Within there you will find several time funtions including difftime(). difftime() will give you the number of seconds between two different dates/time returned as type long. This can be directly translated into the number of days between two dates (divide by the product of 24 * 60 * 60. You can then divide 365 to give an approximation to the number of years. And then the remainder by 30 to get an approximation to the number of months with the remainder of that being the number of days. If you need EXACT number of months and years, then you will need to dig further in time header file than I have dug, or create the function yourself. One of the drawbacks to the time.h file is many of the functions use a struct called tm to hold data, or use type time_t or pointer to type time_t which is a bit of a nuisance until you get used to it.
    Thanks. But I only need to calculate the number of "days" difference between two date. No need to get the month or year different. For example, given 11, 3, 2002 and 9, 3, 2002 (dd mm yyyy), the date difference output should be 2.
    And it needs to be exact too. Probelm arise when the two input are of two different year. I need to find out whehter it is leap year or not. Then need to find out all the day within EACH month. That is so tedious.

  9. #9
    Unregistered
    Guest
    If you use difftime() you don't need to know about leap year. difftime takes that into account as it is counting up the seconds that have occurred between the two date/times. Just divide the double returned by difftime() by the number of seconds in a day to get the number of days. The trick, if there is one, is to convert the input format into a format that can be used by difftime(). Again I refer you to the time header file to get that information.

  10. #10
    Niy
    Guest

    Thumbs up

    oh, Thanks. But using the timediff() seems a bit lazy. hoho.
    Also, reducing my input date to "seconds" seems a bit .... over-doing something. Anyway, if timediff() would return the number of days(integer), that would be best.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting days between 2 dates
    By nynicue in forum C Programming
    Replies: 12
    Last Post: 02-19-2010, 10:50 AM
  2. how can i get the number of days between two dates?
    By acohrockz21 in forum C Programming
    Replies: 3
    Last Post: 02-29-2008, 10:43 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 Cprogramming.com
    Replies: 4
    Last Post: 07-12-2004, 01:52 PM
  5. C++ number of days between 2 dates?
    By Rain1522 in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2002, 09:09 PM

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