Product Site

5.4.6.19. Arithmetic Methods

Note

The syntax diagram above is for the non-prefix operators. For the prefix operators, omit the parentheses and argument.

Returns the result of performing the specified arithmetic operation on the receiver DateTime instance. Depending on the operation, the argument may be either a TimeSpan object or a DateTime instance. See the description of the individual operations for details. The arithmetic_operator can be:
+

Addition. Adds a TimeSpan to the DateTime object, returning a new DateTime instance. The receiver DateTime object is not changed. The TimeSpan may be either positive or negative.
-

Subtraction. If argument is a DateTime object, the two times are subtracted, and a TimeSpan object representing the interval between the two times is returned. If the receiver DateTime is less than the argument argument DateTime, a negative TimeSpan interval is returned. The receiver DateTime object is not changed.

If argument is a TimeSpan object, subtracts the TimeSpan from the DateTime object, returning a new DateTime instance. The receiver DateTime object is not changed. The TimeSpan may be either positive or negative.
Prefix -

A prefix - operation on a DateTime object will raise a SYNTAX error condition.
Prefix +

Returns a new DateTime object with the same time value.

Note

When adding or subtracting DateTime and TimeSpan objects, leap seconds (like the one which happened on December 31, 2016 at 23:59:60 UTC) are not taken into account.
Example 5.231. DateTime class — arithmetic
t = .dateTime~new~timeOfDay       -- returns TimeSpan for current time
say t                             -- displays "17:30:41.482000", perhaps
d = .dateTime~new(2016, 12, 31)   -- creates new date

future = d + t                    -- adds timespan to d
say future                        -- displays "2016-12-31T17:30:41.482000"

-- does not take leap second into account
say d + .TimeSpan~new(24, 0, 1)   -- 2017-01-01T00:00:01.000000

                                  -- "real" start of next century
nextCentury = .dateTime~new(2101, 1, 1)
-- "The next century starts in 29547.07:15:42.721000 days", perhaps
say "The next century starts in" (nextCentury - .dateTime~new) "days"