Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How do I calc a countdown to the date of an event?

  1. Feb 2, 2005 #1
    This is for writing a program btw. The dates in the program are represented by the number of days passed since 12/30/1899.

    Ok, let's say today is 02-Feb-2005 and there is an event that occurs on 01-May-2008. Now by subtracting the day of the event from today's date, I know the number of days remaining till the day of the event is 1184 (including the leap year) but how do I represent this in Years, Months, Days till the event?

    Is there any algorithym that I can use in my program to calc how many years, months, and days until the event occurs? It's a countdown timer sort of like what you'd see like a countdown to the next millenium etc.

    Thanks in Advance,
  2. jcsd
  3. Feb 2, 2005 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    If you're writing this program for a school assignment, you'll likely have to figure out the algorithm by yourself. If it's a personal project, I suggest you just use one of the many time/date packages available for just about every programming language.

    - Warren
  4. Feb 2, 2005 #3
    I'm using C++ builder. I am using the date packages that come with it. But there is no such function that can give you the number of years, months, days to a specific date. I looked all day.

    It's not for school - I got the idea from school when we had to calc the number of days to a given date. Then I thought it would be cool to take that one step further and do a countdown to my birthday or something but in years, months, days rather than just days.

    I'm trying to learn this stuff because it interests me but stuff like this is intimidating. I thought if someone could show me how it's done, I might learn something from it. I wouldn't learn how to do it if it just came in a package that I could never see.
  5. Feb 2, 2005 #4


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member


    Use Google. I typed in "date time class" and came up with a load of good classes. How about this one?

    http://www.codeproject.com/datetime/hightime.asp [Broken]

    - Warren
    Last edited by a moderator: May 1, 2017
  6. Feb 2, 2005 #5


    User Avatar

    Even easier if you want you can just get a free tripod or angelfire site and those always include counting down meters.
  7. Feb 3, 2005 #6
    huh... well I wanted to see how the calc is done. So I can understand it. I'm actually surprised no one here knows how to do it either. No wonder I'm having a hard time figuring it out! I thought it would be a piece of cake for most of the people here. :(

    I don't want a function that does it for me, I want to see how the calculation works. It's one of those things where you've tried to figure it out and then get curious to know how it is actually done rather than just using something to do it for you and never learning.
  8. Feb 3, 2005 #7


    User Avatar
    Staff Emeritus
    Gold Member

    It is not hard at all. You just need to specify what exactly you want to do. I think the only thing to specify is if you are going to count actual calendar months remaining, or just 30-days periods. Tell me, what difference does that make? how would you implement the former (which is easier)? what would you need to add for the latter?
  9. Feb 3, 2005 #8
    Well I can't just divide the number of days remaining by 365.2422 (I've tried this way) to get the years cuz then you'd have to know how many days are in each month left to calc the number of months, and then the remaining days... and I'd have to check if a year is a leap year (which is no problem)... so I don't know how to do it really.

    I can figure out the number of days to the event no problem. All I want is to enter a date in the future. Then I want the calculation to give me the data I need to display something like this:

    2 years, 11 months and 5 days till your 20th birthday!

    or whatever the event may be.

    So each day it would countdown the above. So I need a way to determine the number of years, months, and days between the current date and the date of the event.
    Last edited: Feb 3, 2005
  10. Feb 4, 2005 #9
    Simpler than it looks

    Here is a good link on leap years.

    Other than that, using the language's built in date functions and simple math and data structures should do the rest.

    (e.g. year2-year1=difference_in_years, an array with the number of days in a month, etc)
  11. Feb 4, 2005 #10
    Coupla more links that might be helpful:
    Gotta be acurate!

    The following might help in seeing how they organized their data structures and also provide some good background info:


  12. Feb 4, 2005 #11
  13. Feb 4, 2005 #12
    Thanks, but I didn't really need all that. Too confusing lol. But I figured it out myself anyway. The code is quite simple but figuring out what to do wasn't.

    Here's what I came up with and it seems to work (Hoping I didn't miss anything).

    void __fastcall TForm1::CalcDates()
    years = (event_year - year);
    months = (event_month - month);
    days = (event_day - day);

    if ( days < 0 )
    int monthIndex = (event_month - 1) - 1; // zero based
    int daysInMonth = MonthDays[ IsLeapYear( event_year ) ]
    [ (monthIndex < 0) ? monthIndex+12 : monthIndex ];
    days = (daysInMonth - day) + event_day;
    if ( months < 0 )
    years = (years - 1 < 0) ? 0 : years-1;
    months = months+12;

    Thanks anyway :)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook