Listing 1 time1.c — prints the current date and time in various formats

#include <stdio.h>
#include <time.h>

#define BUFSIZE 128

main()
{
   time_t tval;
   struct tm *now;
   char buf[BUFSIZE];
   char *fancy_format =
     "Or getting really fancy:\n"
     "%A, %B %d, day %j of %Y.\n"
     "The time is %I:%M %p.";

   /* Get current date and time */
   tval = time(NULL);
   now = localtime(&tval);
   printf("The current date and time:\n"
         "%d/%02d/%02d %d:%02d:%02d\n\n",
     now->tm_mon+1, now->tm_mday, now->tm_year,
     now->tm_hour, now->tm_min, now->tm_sec);
   printf("Or in default system format:\n%s\n",
         ctime(&tval));
   strftime(buf,sizeof buf,fancy_format,now);
   puts(buf);

   return 0;
}

/*  Output
The current date and time:
10/06/92 12:58:00

Or in default system format:
Tue Oct 06 12:58:00 1992

Or getting really fancy:
Tuesday, October 06, day 280 of 1992.
The time is 12:58 PM.
*/

/* End of File */