Hi, I was wondering if there is a simple way to get the current date in C?
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?...
Hi, I was wondering if there is a simple way to get the current date in C?
Here is one way - also look up struct tm in help or edit time.h to see what struct tm.
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.Code:#include <time.h> ............. /* code block */ time_t lt; struct tm t, *tmptr; lt=time(NULL); tmptr=localtime( & lt ); printf("%s\n",asctime(tmptr));
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.
Works great. Thanks.