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

◆ add_milliseconds()

date_time xtd::date_time::add_milliseconds ( double  value) const

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

Parameters
valueA number of whole and fractional milliseconds. The value parameter can be negative or positive.
Returns
An object whose value is the sum of the date and time represented by this instance and the number of milliseconds 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 uses the xtd::date_timeadd_milliseconds method to add one millisecond and 1.5 milliseconds to a xtd::date_time value. It then displays each new value and displays the difference between it and the original value. The difference is displayed both as a time span and as a number of ticks. The example makes it clear that one millisecond equals 10,000 ticks. It also shows that fractional milliseconds are rounded before performing the addition; the xtd::date_time value that results from adding 1.5 milliseconds to the original date is 2 milliseconds greater than the original date.
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/startup>
#include <xtd/string>
using namespace xtd;
class program {
public:
static auto main() {
auto date_format = "{0:d}/{0:t}.{0:c}"_s;
auto date1 = date_time {2010, 9, 8, 16, 0, 0};
console::write_line("Original date: {0} ({1:N0} ticks)\n", string::format(date_format, date1), date1.ticks());
auto date2 = date1.add_milliseconds(1);
console::write_line("Second date: {0} ({1:N0} ticks)", string::format(date_format, date2), date2.ticks());
console::write_line("Difference between dates: {0} ({1:N0} ticks)\n", date2 - date1, date2.ticks() - date1.ticks());
auto date3 = date1.add_milliseconds(1.5);
console::write_line("Third date: {0} ({1:N0} ticks)", string::format(date_format, date3), date3.ticks());
console::write_line("Difference between dates: {0} ({1:N0} ticks)", date3 - date1, date3.ticks() - date1.ticks());
}
};
startup_(program::main);
// This code produces the following output :
//
// Original date: 09/08/2010/16:00:00.0000000 (634195584000000000 ticks)
//
// Second date: 09/08/2010/16:00:00.0000000 (634195584000010000 ticks)
// Difference between dates: Mon Jan 1 00:00:00 0001 (10000 ticks)
//
// Third date: 09/08/2010/16:00:00.0005000 (634195584000015000 ticks)
// Difference between dates: Mon Jan 1 00:00:00 0001 (15000 ticks)
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.h:85
#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
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
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 fractional part of value is the fractional part of a millisecond. For example, 4.5 is equivalent to 4 milliseconds and 5000 ticks, where one millisecond = 10000 ticks.
The value parameter is rounded to the nearest integer.