Days elapsed in a year

This is a discussion on Days elapsed in a year within the C++ Programming forums, part of the General Programming Boards category; Ok I have tried to search this forum and other places but to no avail. I'm making a library system ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    Red face 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.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also, the localtime() function has a structure member called tm_yday (day of year).

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    Thumbs up

    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 );
        
    newtime->tm_yday;
        
    cout << << 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Calendar Problem
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-29-2002, 03:36 PM
  3. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 10:45 PM
  4. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 06:23 AM
  5. Counting Number of days from year zero
    By wireless in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21