🚀 Welcome to MDriven Learn –  Don’t miss the latest Release Notes.
OCLOperators Add(DateTime, TimeSpan and Decimal)

This page covers .Add on DateTime, TimeSpan and Decimal values. To add an element to a collection or association, see add (->add).

.Add adds one value to another. The type of the result depends on the types you use it with.

Syntax

aDecimal.Add(anotherDecimal)
aDateTime.Add(aTimeSpan)
aTimeSpan.Add(anotherTimeSpan)

Returns

Used on Argument Returns
Decimal Decimal Decimal
DateTime TimeSpan DateTime
TimeSpan TimeSpan TimeSpan

Examples

Decimal + Decimal → Decimal

Decimal.Parse('10.5').Add(Decimal.Parse('2.25'))

Result: 12.75

In the context of SalaryStructure:

self.BaseSalary.Add(Decimal.Parse('500'))

DateTime + TimeSpan → DateTime

DateTime.Parse('2026-11-20 08:15:30').Add(TimeSpan.FromHours(30))

Result: 11/21/2026 2:15:30 PM

Adding the difference between two dates back onto the earlier date returns the later date:

DateTime.Parse('2026-11-20 08:15:30').Add(DateTime.Parse('2026-11-23 14:30:00').Subtract(DateTime.Parse('2026-11-20 08:15:30')))

Result: 11/23/2026 2:30:00 PM

TimeSpan + TimeSpan → TimeSpan

TimeSpan.FromHours(5).Add(TimeSpan.FromMinutes(30))

Result: 05:30:00

Notes

  • OCL method names are not case-sensitive: .add(...) and .Add(...) give the same result. For example, DateTime.Parse('2026-11-20 08:15:30').add(TimeSpan.FromHours(30)) also returns 11/21/2026 2:15:30 PM.
  • On a Decimal, the argument must also be a Decimal. A plain number literal such as 2.25 is read as a Double and fails with There is no version of Add that takes these parameter types (System.Decimal.Add(System.Double)). Wrap it with Decimal.Parse('2.25') or use another Decimal attribute.
  • .Add does not work on Double values (plain number literals). Use the + operator for those.
  • On a DateTime, .Add takes a TimeSpan and returns a DateTime. To move a date by a fixed number of units, the AddDays, AddHours and related operations are simpler.
  • To go back in time, pass a negative TimeSpan, or use Subtract.

See also