Thread: Date Function

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    Date Function

    Hi, I was wondering if there is a simple way to get the current date in C?

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    Here is one way - also look up struct tm in help or edit time.h to see what struct tm.
    Code:
    #include <time.h>
    .............
    /* code block */
       time_t lt;
       struct tm t, *tmptr;
       lt=time(NULL);
       tmptr=localtime( & lt );
       printf("%s\n",asctime(tmptr));
    Most compilers keep a static string that asctime or ctime populates, and the contents of the string changes with each new call to asctime- you need to make your own copy of the string.

    Note: to get the characters to display I added extra spaces around the ampersand and lt in localtime()
    Last edited by jim mcnamara; 01-10-2004 at 06:09 AM.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    Works great. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. function to return the date in a string
    By fborot in forum C Programming
    Replies: 10
    Last Post: 12-12-2005, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM