Dominic Bou-Samra Dominic Bou-Samra - 1 year ago 101
C Question

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

What's the best method to print out time in C in the format


Answer Source

Use strftime().

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

int main()
    time_t timer;
    char buffer[26];
    struct tm* tm_info;

    tm_info = localtime(&timer);

    strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);

    return 0;

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download