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

1. Feb 2, 2005

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.

Jen

2. Feb 2, 2005

### chroot

Staff Emeritus
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

3. Feb 2, 2005

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.

4. Feb 2, 2005

### chroot

Staff Emeritus

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

- Warren

Last edited by a moderator: May 1, 2017
5. Feb 2, 2005

### sdp

Even easier if you want you can just get a free tripod or angelfire site and those always include counting down meters.

6. Feb 3, 2005

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.

7. Feb 3, 2005

### ahrkron

Staff Emeritus
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?

8. Feb 3, 2005

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
9. Feb 4, 2005

### cosmicDASD

Simpler than it looks

Here is a good link on leap years.
http://www.timeanddate.com/date/leapyear.html

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)

10. Feb 4, 2005

### cosmicDASD

Gotta be acurate!
http://www.boulder.nist.gov/timefreq/service/its.htm

The following might help in seeing how they organized their data structures and also provide some good background info:
http://www.ietf.org/rfc/rfc3339.txt

http://en.wikipedia.org/wiki/ISO_8601#Durations

http://en.wikipedia.org/wiki/Calculating_the_Day_of_the_Week
http://www.merlyn.demon.co.uk/zel-1886.htm

11. Feb 4, 2005

### cosmicDASD

12. Feb 4, 2005

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 )
{
months--;
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 :)