Untitled

 avatar
unknown
plain_text
a year ago
1.4 kB
6
Indexable
#include <iostream>
#include <iomanip>
#include <chrono>
#include <sstream>

// Function to add or subtract days from a given date
std::chrono::system_clock::time_point addDays(std::chrono::system_clock::time_point date, int days) {
    return date + std::chrono::hours(days * 24);
}

// Function to parse a date from day, month, and year
std::chrono::system_clock::time_point parseDate(int day, int month, int year) {
    std::tm tm = {};
    tm.tm_mday = day;
    tm.tm_mon = month - 1; // Months are 0-based in struct tm
    tm.tm_year = year - 1900; // Years since 1900 in struct tm
    return std::chrono::system_clock::from_time_t(std::mktime(&tm));
}

// Function to format and print a date
void printDate(std::chrono::system_clock::time_point date) {
    std::time_t time = std::chrono::system_clock::to_time_t(date);
    std::tm* tm = std::localtime(&time);
    std::cout << std::put_time(tm, "%d/%m/%Y") << std::endl;
}

int main()
{
  int difference;
  int day, month, year;

  std::cout << "Enter the difference in days: ";
  std::cin >> difference;
  std::cout << "Enter a date in the form day month year: ";
  std::cin >> day >> month >> year;
  
  auto inputDate = parseDate(day, month, year);
  
  auto newDate = addDays(inputDate, difference);
  
  // Display the resulting date
  std::cout << "The final date is: ";
  printDate(newDate);

  return 0;
  
}
Editor is loading...
Leave a Comment