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

  • Thread starter StupidHead
  • Start date
In summary: InMonth; } if ( months < 0 ) { years--; months += 12; } if ( years < 0 ) { // do something here }}//---------------------------------------------------------------------------In summary, the conversation discusses the process of calculating the number of years, months, and days until a specific date, such as a birthday or event. The individual asking for help is using C++ builder and is looking for a way to represent this countdown in years, months, and days rather than just days. They have tried various methods such as using a time/date package and dividing the number of days remaining by 365.2422, but have had difficulty factoring in
  • #1
StupidHead
19
0
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,
Jen
 
Mathematics news on Phys.org
  • #2
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
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
StupidHead:

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

- Warren
 
Last edited by a moderator:
  • #5
Even easier if you want you can just get a free tripod or angelfire site and those always include counting down meters.
 
  • #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.
 
  • #7
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
Well I can't just divide the number of days remaining by 365.2422 (I've tried this way) to get the years because 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:
  • #9
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
cosmicDASD said:
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)
Coupla more links that might be helpful:
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
 
  • #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 )
{
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 :)
 

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

What is a countdown?

A countdown is a way to track the number of days, hours, minutes, and seconds remaining until a specific event or deadline.

How do I calculate a countdown?

To calculate a countdown, you will need to determine the current date and time, as well as the date and time of the event. Then, use a mathematical formula to calculate the difference between the two dates and times, taking into account any time zone differences.

What is the formula for calculating a countdown?

The formula for calculating a countdown is: (Event Date and Time - Current Date and Time) = Remaining Time. This can be calculated using a variety of methods, including using a calendar or using a programming language such as JavaScript.

How accurate is a countdown?

The accuracy of a countdown depends on the accuracy of the current date and time and the date and time of the event. If there are any discrepancies or changes in the event date or time, the countdown may not be entirely accurate.

Can I customize a countdown?

Yes, you can customize a countdown by adding different features, such as changing the font or color, adding a background image, or including a message or image related to the event. There are also many countdown widgets and apps available that allow for customization.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
Replies
9
Views
1K
  • Computing and Technology
Replies
25
Views
3K
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
Replies
2
Views
3K
  • Special and General Relativity
3
Replies
75
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Replies
17
Views
4K
  • STEM Academic Advising
Replies
10
Views
1K
Back
Top