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

前端 未结 7 2037
旧时难觅i
旧时难觅i 2020-11-27 12:24

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

7条回答
  •  北海茫月
    2020-11-27 12:43

    time.h defines a strftime function which can give you a textual representation of a time_t using something like:

    #include 
    #include 
    int main (void) {
        char buff[100];
        time_t now = time (0);
        strftime (buff, 100, "%Y-%m-%d %H:%M:%S.000", localtime (&now));
        printf ("%s\n", buff);
        return 0;
    }
    

    but that won't give you sub-second resolution since that's not available from a time_t. It outputs:

    2010-09-09 10:08:34.000
    

    If you're really constrained by the specs and do not want the space between the day and hour, just remove it from the format string.

提交回复
热议问题