Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i add a certain number of days to a current date in c

Tags:

c

I've been trying to add a certain number of days (I'm trying to add 30) to the current date.

Let's say today's date is 1/1/2023, after adding the number of days I want to add, I would get 31/1/2023, the main problem would be if I wanted to add if February has 29 days instead of 28.

I got the current date, couldn't add the number of days I wanted because I don't know other way to get the current date without the format being like this "Sun Jan 01 16:29:19 2023".

Just a side note, I saw people adding x days to a date using c, but those problem had the user inserting the date they wanted to the problem, my problem is adding it to the CURRENT DATE.

Anyone can help me with this one?

like image 569
sprata Avatar asked Oct 27 '25 08:10

sprata


1 Answers

The standard library time functions already know about leap years and number of days in a month etc. So it makes sense to use that calendrical "expertise".

This is a "duplicate" of my answer at Algorithm to add or subtract days from a date? but adapted slightly to remove the C++ specifics (hence not marking it as a duplicate). Other answers there may be adapted of course.

#include <time.h>

// Adjust date by a number of days +/-
void DatePlusDays( const struct tm* date, int days )
{
    const time_t ONE_DAY = 24 * 60 * 60 ;

    // Seconds since start of epoch
    time_t date_seconds = mktime( date ) + (days * ONE_DAY) ;

    // Update caller's date
    // Use localtime because mktime converts to UTC so may change date
    *date = *localtime( &date_seconds ) ;
}

Regarding:

"I saw people adding x days to a date using c, but those problem had the user inserting the date they wanted to the problem, my problem is adding it to the CURRENT DATE."

So surely you need only supply the current date as the input. No need to restrict the solution to be quite so inflexible.

Example usage for "current date":

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

int main( void )
{
    struct tm today ;
    today = *localtime( time(NULL) ) ;

    // Date, plus 30 days
    DatePlusDays( &today, 30 ) ; 

    // Show time/date using default formatting
    printf( "%s\n", asctime( &today) ) ;
}

It can be used to subtract days too (pass negative days).

Of course, if you truly do want to restrict the solution to just the current date:

#include <time.h>

// Adjust date by a number of days +/-
const struct tm* TodayPlusDays( int days )
{
    const time_t ONE_DAY = 24 * 60 * 60 ;

    // Seconds since start of epoch
    time_t date_seconds = time( NULL ) + (days * ONE_DAY) ;

    // Update caller's date
    // Use localtime because mktime converts to UTC so may change date
    return localtime( &date_seconds ) ;
}

Example usage:

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

int main( void )
{
    struct tm today = *TodayPlusDays( 30 ) ;

    // Show time/date using default formatting
    printf( "%s\n", asctime( &today) ) ;
}
like image 58
Clifford Avatar answered Oct 28 '25 21:10

Clifford