Thread: Dates

  1. #1
    Chiz
    Guest

    Dates

    Is there any better way of getting the system date values and storing them than this? The values have got to be stored as type int for the work i'll be doing.

    Code:
    #include <iostream.h>
    #include <time.h>
    
    int main()
    {
      const int DATE_LEN=5;
      int Day, Month, Year;
    
      static char Date[DATE_LEN];
      time_t SystemDate;
    
      SystemDate = time(NULL);
                 
      strftime(Date, sizeof(Date), "%d", localtime(&SystemDate));
      Day = atoi(Date);
      strftime(Date, sizeof(Date), "%m", localtime(&SystemDate));
      Month = atoi(Date);
      strftime(Date, sizeof(Date), "%Y", localtime(&SystemDate));
      Year = atoi(Date);
    
      cout << "The date is: Day " << Day << " Month " << Month << " Year " << Year;
    
      cin.get();
      return 0;
    }

  2. #2
    Chiz
    Guest
    Would someone be able to comment on the code above, is there a more efficent way of storing the day, month and year? Or is this code ok?

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    18

    Lightbulb Date.

    Try this:

    #include <dos.h>
    #include <stdio.h>

    int main(void)
    {
    struct date d;

    getdate(&d);
    printf("The current year is: %d\n", d.da_year);
    printf("The current day is: %d\n", d.da_day);
    printf("The current month is: %d\n", d.da_mon);
    return 0;
    }
    Language: C++
    Compiler: Borland C++
    OS: Windows NT

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main ( ) {
        time_t  now;
        struct tm tm_now;
    
        now = time(NULL);
        tm_now = *localtime(&now);
        printf( "Day=%d, Month=%d, year=%d\n",
            tm_now.tm_mday, tm_now.tm_mon+1, tm_now.tm_year+1900 );
        return 0;
    }
    Shows you the tm fields containing the information you want

  5. #5
    Chiz
    Guest
    Thanks for your replies, the code is more efficient. Salem, just wondering if you could explain why you have to declare two structures to hold the information from the system clock, time_t and tm? Would be grateful for an explanation thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting days between 2 dates
    By nynicue in forum C Programming
    Replies: 12
    Last Post: 02-19-2010, 10:50 AM
  2. Getting the difference between dates
    By Leftos in forum C Programming
    Replies: 7
    Last Post: 01-20-2008, 12:49 AM
  3. Using dates in Pro*C
    By clancyPC in forum C Programming
    Replies: 0
    Last Post: 08-17-2006, 06:37 AM
  4. number of days between 2 dates.
    By explosive in forum C++ Programming
    Replies: 10
    Last Post: 02-17-2005, 07:30 AM
  5. Comparing Dates?
    By Excalibur XP in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 09:00 PM