xtd 0.2.0
Loading...
Searching...
No Matches

◆ add()

date_time xtd::date_time::add ( const xtd::time_span value) const

Returns a new xtd::date_time that adds the value of the specified xtd::time_span to the value of this instance.

Parameters
valueA positive or negative time interval.
Returns
An object whose value is the sum of the date and time represented by this instance and the time interval represented by value.
Exceptions
xtd::argument_out_of_range_exceptionThe resulting xtd::date_time is less than xtd::date_time::min_value or greater than xtd::date_time::max_value.
Examples
The following example demonstrates the xtd::date_time::add method. It calculates the day of the week that is 36 days (864 hours) from this moment.
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/startup>
#include <chrono>
class program {
public:
static auto main() {
// Calculate what day of the week is 36 days from this instant.
auto today = xtd::date_time::now();
auto duration = std::chrono::days {36};
auto answer = today.add(duration);
xtd::console::write_line("{0}", today);
xtd::console::write_line("{0}", answer);
}
};
startup_(program::main);
// This code can produce the following output :
//
// Wed Jan 12 14:06:34 2022
// Thu Feb 17 14:06:34 2022
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static date_time now() noexcept
Gets a xtd::date_time object that is set to the current date and time on this computer,...
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
Remarks
You can use the xtd::date_time::add method to add more than one kind of time interval (days, hours, minutes, seconds, or milliseconds) in a single operation. This method's behavior is identical to that of the addition operator. The xtd::date_time::addxtd::date_time structure also supports specialized addition methods (such as xtd::date_time::add_days, xtd::date_time::add_hours, and xtd::date_time::add_minutes) for each time interval.
The xtd::date_time::add method takes into account leap years and the number of days in a month when performing date arithmetic.
This method does not change the value of this xtd::date_time. Instead, it returns a new xtd::date_time whose value is the result of this operation. The xtd::date_time::kind property of the new xtd::date_time instance is the same as that of the current instance.