Skip to content

AddTimeSpan

Returns a new DateTimeOffset by adding a specified duration, considering calendar rules.

1
myCalendar.AddTimeSpan(dateTime: [DateTimeOffset](./../DateTimeOffset/index.md), duration: string):DateTimeOffset

Parameters

DateTimeOffset dateTime
    The base DateTimeOffset value to add the duration to.

string duration
    The duration to add, in either "d.hh:mm:ss" format or ISO-8601 duration format.

Returns

A new DateTimeOffset value representing the result of the addition, considering calendar rules.

Remarks

This method adds the given duration in business hours. If you only need to add the duration and find the next business date and time, please use the Calendar.GetDateTime method instead.

In addition to the ISO-8601 duration format, this method also supports begin and end specifiers for time periods:

  • BD: Begin of the day
  • DE: End of the day
  • MB: Begin of the month
  • ME: End of the month
  • YB: Begin of the year
  • YE: End of the year

Examples:

  • P2BY: Adds 2 years to the current date and rolls back to the beginning of the year.
  • P1M2BD: Adds 1 month and 2 days to the current date and rolls back to the beginning of the day.
  • P1M2ED: Adds 1 month and 2 days to the current date and forwards to the end of the day.

Add an ISO-8601 duration using business-hour rules

1
2
// Adds 1 month, 1 day, and 2 hours
var newDate = $Calendar.StandardCalendar.AddTimeSpan($Calendar.Today, 'P1M1DT2H');

Add a time span using day-hour-minute-second format

1
2
// Adds 1 day, 2 hours, 30 minutes, and 45 seconds to the current time.
var newDate = $Calendar.StandardCalendar.AddTimeSpan($Calendar.Today, '1.02:30:45');

Warning

This method adds the duration in business hours and may skip non-business dates while adding the duration.

See how business hours shift the resulting time

1
2
var nextDate = $Calendar.AddTimeSpan(DateTimeOffset.parse('2023-02-08T17:00'), 'PT2H');
// nextDate will be set to '2020-02-09T10:00'

Compare equivalent duration formats

1
2
var newDate = $Calendar.StandardCalendar.AddTimeSpan($Calendar.Today, '1.02:30:45');
var newDate = $Calendar.StandardCalendar.AddTimeSpan($Calendar.Today, 'P1DT2H');

See Also