How to print date in C?

I am trying to print a date from a string like "01/01/01" and get something like "Monday, January 2001.

I found something with the ctime person, but I really don’t understand how to use it.

Any help?

Thanks,

+7
c date ctime
source share
3 answers

You can use strptime to convert string date to struct tm

 struct tm tm; strptime("01/26/12", "%m/%d/%y", &tm); 

And then type struct tm in the appropriate date format with strftime

 char str_date[256]; strftime(str_date, sizeof(str_date), "%A, %d %B %Y", &tm); printf("%s\n", str_date); 
+5
source share

strftime() does the job.

 char buffer[256] = ""; { struct tm t = <intialiser here>; strftime(buffer, 256, "%H/%M/%S", &t); } printf("%s\n", buffer); 
+4
source share

You may be looking for strftime

+1
source share

All Articles