-
Days elapsed in a year
Ok I have tried to search this forum and other places but to no avail. I'm making a library system for a project at uni and want to log the dates books are borrowed from and until as the number of the day of that year. So 31st Jan 2001 will be 31 and 15 Feb 2001 will be 46 etc. Is there something that will do this for me or am I going to have to bodge something together? I'm not too fussed about rolling into the next year or leap years at the mo.
Thanks.
-
Most systems have a routine to retrieve the date/time from the system clock. The time() function for example, (look in the help).
This gives the number of seconds that have elapsed since midnight 01/01/1970, (it should anyway, check your docs). Take the date you want to use as your start point and subtract that value from the value returned by time, then any later time will be n seconds into the period you are interested in, dividing that by 86400, (number of seconds in a day), will tell you how many days have elapsed since your origin.
There may be an easier way in your OS's API but you don't say what you are using.
-
Also, the localtime() function has a structure member called tm_yday (day of year).
-
Thanks for that. I've now got it using this:
PHP Code:
time_t long_time;
struct tm *newtime;
int i;
time( &long_time );
newtime = localtime( &long_time );
i = newtime->tm_yday;
cout << i << endl;
Which prints 99 Even though it's the hundredth day today, guess I'll just add 1. Which is exactly what I wanted. Thanks a lot :)