How to add N weeks to the current date in C++

2 Answers

0 votes
#include <iostream>
#include <chrono>

// C++20

/**
 * Adds a specified number of weeks to the current system date.
 *
 * @param weeks The number of weeks to add (supports negative values to subtract).
 * @return std::chrono::year_month_day The resulting calendar date.
 */
std::chrono::year_month_day addWeeksToCurrentDate(int weeks) 
{
    // 1. Get the current system time point
    auto now = std::chrono::system_clock::now();

    // 2. Truncate the time point to full days (dropping hours, minutes, seconds)
    auto today = std::chrono::floor<std::chrono::days>(now);

    // 3. Convert to sys_days, which supports direct arithmetic operations
    // 1 week automatically converts safely to 7 days
    std::chrono::sys_days target_day = today + std::chrono::weeks(weeks);

    // 4. Convert the resulting time point back to a calendar year-month-day format
    return std::chrono::year_month_day{target_day};
}

int main() 
{
    int weeks_to_add = 4;
    
    // Calculate the future date
    std::chrono::year_month_day future_date = addWeeksToCurrentDate(weeks_to_add);

    // Extract year, month, and day components safely
    int year = static_cast<int>(future_date.year());
    unsigned int month = static_cast<unsigned>(future_date.month());
    unsigned int day = static_cast<unsigned>(future_date.day());

    // Output formatted as YYYY-MM-DD
    std::cout << "Date after adding " << weeks_to_add << " weeks: " 
              << year << "-" 
              << (month < 10 ? "0" : "") << month << "-" 
              << (day < 10 ? "0" : "") << day << "\n";
}


/*
run:

Date after adding 4 weeks: 2026-12-05

*/

 



answered 1 day ago by avibootz
0 votes
#include <iostream>
#include <chrono>
#include <iomanip>
#include <ctime>

// C++11/14/17

/**
 * Adds a specified number of weeks to the current system date using legacy C-style structures.
 *
 * @param weeks      The number of weeks to add.
 * @param buffer     Character array buffer to store the resulting date string.
 * @param bufferSize The size of the buffer.
 */
void addWeeks(int weeks, char* buffer, size_t bufferSize) 
{
    // Get current system time as a time_t epoch value
    auto now = std::chrono::system_clock::now();
    std::time_t now_c = std::chrono::system_clock::to_time_t(now);

    // Convert to local time structure
    std::tm* time_info = std::localtime(&now_c);

    // Add weeks by adjusting days directly (1 week = 7 days)
    time_info->tm_mday += weeks * 7;

    // mktime automatically normalizes the date (handles month/year overflows)
    std::time_t future_c = std::mktime(time_info);

    // Format output as YYYY-MM-DD
    std::strftime(buffer, bufferSize, "%Y-%m-%d", std::localtime(&future_c));
}

int main() 
{
    // Define the number of weeks to add
    int weeks_to_add = 4;

    // Allocate a buffer large enough for "YYYY-MM-DD" plus the null-terminator (\0)
    char date_buffer[11];

    // Call the function to compute the future date
    addWeeks(weeks_to_add, date_buffer, sizeof(date_buffer));

    // Output the results to the console
    std::cout << "Adding " << weeks_to_add << " weeks to the current date gives: " 
              << date_buffer << "\n";
}


/*
run:

Adding 4 weeks to the current date gives: 2026-12-05

*/

 



answered 1 day ago by avibootz
...