gpt4 book ai didi

c - 使用日期和时间函数

转载 作者:行者123 更新时间:2023-11-30 15:29:58 26 4
gpt4 key购买 nike

此代码用于获取当前日期并显示它

#include stdio.h
#include time.h

int main(void)

{

char currentt[80];

time_t now = time(NULL);

struct tm *t = localtime(&now);

strftime (currentt,80,"%d/%m/%Y",t+=30);

puts (currentt);

printf("%s",currentt);

return 0;

}

我还有另一个代码,可以在手动输入的日期后添加 30 天

#include stdio.h
#include time.h

int main()

{

/* initialize */
int y=2014, m=9, d=19;
struct tm t = { .tm_year=y-1900, .tm_mon=m-1, .tm_mday=d };
/* modify */
t.tm_mday += 30;
mktime(&t);
/* show result */
printf(asctime(&t));
return 0;
}

我想要做的是以这样的方式合并此代码,以便它获取当前日期第一个代码并使用第二个代码添加 30 天....谁能帮我这个。任何其他逻辑也将受到赞赏,但我希望它是 C 语言的。

最佳答案

第一#include应与 < 一起使用和>文件名周围。下面的代码与上面两段代码类似。我已在适当的地方发表了评论。它只是获取当前时间,在日期字段中添加 30 天,重新计算新时间并输出

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

int main()
{
/* Get the current time*/
time_t now = time(NULL);
struct tm *t = localtime(&now);

/* modify current time by adding 30 days*/
t->tm_mday += 30;
mktime(t);

/* show result */
printf(asctime(t));
return 0;
}

关于c - 使用日期和时间函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25945028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com