Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print time in format: 2009‐08‐10 18:17:54.811

Tags:

c

time

What's the best method to print out time in C in the format 2009‐08‐10 
18:17:54.811?

like image 817
Dominic Bou-Samra Avatar asked Sep 09 '10 01:09

Dominic Bou-Samra


People also ask

How do I print the time in printf?

printf("The time is: %02d:%02d:%02d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); We use the tm_hour , tm_min , and tm_sec members to express a human-readable time format.


1 Answers

Use strftime().

#include <stdio.h> #include <time.h>  int main() {     time_t timer;     char buffer[26];     struct tm* tm_info;      timer = time(NULL);     tm_info = localtime(&timer);      strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);     puts(buffer);       return 0; } 

For milliseconds part, have a look at this question. How to measure time in milliseconds using ANSI C?

like image 159
Hamid Nazari Avatar answered Sep 29 '22 22:09

Hamid Nazari