c - 如何在C中计算x天后的日期

标签 c date days real-time-clock

我正在用 C 编写一个微 Controller ,它有一个内部 RTC 并自动递增一个日计数器 (0-65536)。因此,鉴于用户调整的初始日期 (DD/MM/YYYY),我需要根据该计数器更新日历。也就是说,我需要知道如何计算 x 天后的日期。有谁知道一个算法吗?在整个网络上找不到任何内容。

提前致谢。 丹尼尔

最佳答案

正如@moooeeeeep 在 his answer 中建议的那样同样的问题,

For the sake of clarity and correctness, you should stick to the existing solutions.

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

int main()
{        
    /* initialize */
    int y=1980, m=2, d=5;    
    struct tm t = { .tm_year=y-1900, /* The number of years since 1900   */ 
                    .tm_mon=m-1,  /* month, range 0 to 11 */
                    .tm_mday=d };
    /* modify */
    t.tm_mday += 40;
    mktime(&t);
    /* show result */
    printf("%s", asctime(&t)); /* prints: Sun Mar 16 00:00:00 1980 */
    return 0;
}

关于c - 如何在C中计算x天后的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13209097/

相关文章:

根据R中的日期变量重新组织多个变量

java - 在 Java 中使用 SimpleDateFormat 日期

ios - 从日期选择器开始的 Swift CountDown 天数

c - 打印第一位带零的数字

c - 这个无锁栈的pop函数中的free()安全吗?

c - 扫描文本迫使我进入下一行,以确认输入。对于特殊情况我可以阻止这种情况吗?

c++ - 两种数字比较方式哪一种更有效

apache-spark - PySpark 连接 ID,然后连接 'date' 列中的年份和月份

javascript - 获取用户选择日期的最后 7 天

java - 如何从某个日期获取日期列表?