Arithmetics on calendar dates in C or C++ (add N days to given date)

后端 未结 8 1085
误落风尘
误落风尘 2020-12-06 07:47

I have been given a date, Which I am taking as an input like (day, month, year): 12, 03, 87.

Now I need to find out the date after n days.<

8条回答
  •  温柔的废话
    2020-12-06 08:21

    It might be easier to do the math using seconds since the epoch instead of manipulating the date fields directly.

    For example, this program prints the date 7 days from now:

    #include 
    #include 
    
    main()
    {
        time_t t;
        struct tm *tmp;
    
        time(&t);
    
        /* add a week to today */
        t += 7 * 24 * 60 * 60;
    
        tmp = localtime(&t);
        printf("%02d/%02d/%02d\n", tmp->tm_mon+1, tmp->tm_mday,
            tmp->tm_year % 100);
    }
    

提交回复
热议问题