如何在c++获取指定天数如(2015-04-04)的struct tm?

比较复杂的也有想到 可是真的太复杂 结构体中每个变量都要计算 那有没有什么简便的方法呢?

阅读 3.2k
2 个回答

只有自己构建struct或者class吧,可以搜搜看看有没有第三方的库,不像Java,有比较丰富的Date类型。

你是在说mktime么?

#include <stdio.h>
#include <time.h>
 
int main(void)
{
    struct tm tm = *localtime(&(time_t){time(NULL)});
    printf("Today is           %s", asctime(&tm));
    tm.tm_mon -= 100;  
    mktime(&tm);
    printf("100 months ago was %s", asctime(&tm));
}

输出:

Today is Sun Oct 4 19:11:45 2015
100 months ago was Mon Jun 4 19:11:45 2007

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题