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


by StupidHead
Tags: calc, countdown, date, event
StupidHead
StupidHead is offline
#1
Feb2-05, 03:37 PM
P: 19
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
Phys.Org News Partner Mathematics news on Phys.org
Researchers help Boston Marathon organizers plan for 2014 race
'Math detective' analyzes odds for suspicious lottery wins
Pseudo-mathematics and financial charlatanism
chroot
chroot is offline
#2
Feb2-05, 03:40 PM
Emeritus
Sci Advisor
PF Gold
chroot's Avatar
P: 10,424
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
StupidHead
StupidHead is offline
#3
Feb2-05, 06:35 PM
P: 19
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.

chroot
chroot is offline
#4
Feb2-05, 07:18 PM
Emeritus
Sci Advisor
PF Gold
chroot's Avatar
P: 10,424

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


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
sdp
sdp is offline
#5
Feb2-05, 07:40 PM
P: 4
Even easier if you want you can just get a free tripod or angelfire site and those always include counting down meters.
StupidHead
StupidHead is offline
#6
Feb3-05, 01:44 AM
P: 19
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.
ahrkron
ahrkron is offline
#7
Feb3-05, 02:30 AM
Emeritus
PF Gold
P: 734
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?
StupidHead
StupidHead is offline
#8
Feb3-05, 03:11 AM
P: 19
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.
cosmicDASD
cosmicDASD is offline
#9
Feb4-05, 07:41 PM
P: 3
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)
cosmicDASD
cosmicDASD is offline
#10
Feb4-05, 09:12 PM
P: 3
Quote Quote by cosmicDASD
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/Calcula...ay_of_the_Week
http://www.merlyn.demon.co.uk/zel-1886.htm
cosmicDASD
cosmicDASD is offline
#11
Feb4-05, 09:32 PM
P: 3
Okay, 2 more.

The first one is a perpetual calendar in JavaScript.

If you can get past the JavaScript & other web stuff, you can see you this person does it.

http://www.geocities.com/CapeCanaver...4/perpcale.htm

Do a view source to see it.

Also, they have a neat printable perpetual calendar:
http://www.geocities.com/CapeCanaver.../calemini2.htm

Again, they don't show you exactly, but they are good examples for dealing with date data.
StupidHead
StupidHead is offline
#12
Feb4-05, 11:43 PM
P: 19
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 :)


Register to reply

Related Discussions
Independent event and mutually exclusive event Calculus & Beyond Homework 3
A Game of Countdown General Discussion 6
Countdown... never the same again. General Discussion 0
Gravity probe launching countdown Special & General Relativity 2