Thread: how to get system date

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    184

    how to get system date

    hello guys

    can any one tell how to get a system date. is there any function to get system date. as because of this i am stuck with my uni ass

    any help will be appriciated

    s.s.harish

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You'll want to use the functions in time.h. Here's a good reference:

    http://www.infosys.utas.edu.au/info/...ib.html#time.h

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    so if i declare a instnace of the struct tm i will able to get the vales of the date. is thats something like this

    struct tm t;

    t.tm_mday;
    t.tmmon;
    t.tm_year;

    is this correct.

    s.s.harish

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well, that's a data structure for holding time information. There's a function (I think it's time()) that will actually return that structure filled with the current information. There are then numerous formatting functions you can use. And tm might be typedef'ed - I'm not sure, so just be aware.

    And before posting code, please read the sticky at the top of the C forum regarding posting code, and learn how to use code tags.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    thax very much sean_mackrory

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's a simple example.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
       time_t now = time(NULL);
       struct tm *t = localtime(&now);
       printf( "%d\n", t->tm_mon+1 );
       printf( "%d\n", t->tm_mday );
       printf( "%d\n", t->tm_year+1900 );
       printf( "%d\n", t->tm_hour );
       printf( "%d\n", t->tm_min );
       printf( "%d\n", t->tm_sec );
    
       return 0;
    }
    There's also a function called strftime(), which will format a time structure pointer (tm) to a string.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    allright swoopy
    i got it. cheers swoopy

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    By the way, here's an example of how to use strftime().
    Code:
       time_t now = time(NULL);
       struct tm *t = localtime(&now);
       char date_time[30];
       strftime( date_time, sizeof(date_time), "%x %X", t );
       printf( "%s\n",date_time );
    There's a good library reference here:
    www.cppreference.com
    Last edited by swoopy; 03-03-2005 at 09:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculate age from date
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-22-2005, 08:57 AM
  2. Getting system Date
    By Sridar in forum C++ Programming
    Replies: 4
    Last Post: 08-10-2005, 08:51 PM
  3. bad system date
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 04-30-2003, 08:29 AM
  4. reading the system date
    By Alicia in forum C++ Programming
    Replies: 5
    Last Post: 02-13-2002, 03:18 PM
  5. System Date
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2001, 06:03 PM