Date Function

This is a discussion on Date Function within the C Programming forums, part of the General Programming Boards category; Hi, I was wondering if there is a simple way to get the current date in C?...

  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 05: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, 06:18 AM
  3. function to return the date in a string
    By fborot in forum C Programming
    Replies: 10
    Last Post: 12-12-2005, 12:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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