Question 13.12

How can I get the current date or time of day in a C program?


Just use the time, ctime, and/or localtime functions. (These routines have been around for years, and are in the ANSI standard.) Here is a simple example:

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

main()
{
	time_t now;
	time(&now);
	printf("It's %.24s.\n", ctime(&now));
	return 0;
}

References: K&R2 Sec. B10 pp. 255-7
ANSI Sec. 4.12
ISO Sec. 7.12
H&S Sec. 18


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback