关于strftime的问题

时间:2010-02-21 06:19:34

标签: c++ c datetime time

问题很简单“假设我们有一个整数 1< = n< = 12 ,如何使用strftime显示一月代表' 1 ', 2月代表' 2 ', 3月代表' 3 '和那么......?“

2 个答案:

答案 0 :(得分:4)

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

size_t monthName( char* buf, size_t size, int month)
{
    struct  tm t = {0};

    t.tm_mon = month - 1;   // turn month 1..12 to 0..11 as `struct tm` wants

    return strftime( buf, size, "%B", &t);
}


int main(int argc, char* argv[])
{
    char buf[10];

    monthName( buf, sizeof( buf), 9);
    printf( "%s\n", buf);
    return 0;
}

答案 1 :(得分:2)

struct tm tm = {0};
tm.tm_mon = n - 1;
strftime(s, len, "%B", &tm);