🚀 Welcome to MDriven Learn –  Don’t miss the latest Release Notes.
DateTime Operators

DateTime and TimeSpan values work with the standard arithmetic and comparison operators in OCL.

Arithmetic operators

Expression Returns Same as
aDateTime - anotherDateTime TimeSpan aDateTime.Subtract(anotherDateTime)
aDateTime - aTimeSpan DateTime aDateTime.Subtract(aTimeSpan)
aDateTime + aTimeSpan DateTime aDateTime.Add(aTimeSpan)

Examples

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

Result: 3.06:14:30

DateTime.Parse('2026-11-23 14:30:00') - TimeSpan.FromHours(30)

Result: 11/22/2026 8:30:00 AM

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

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

Hours passed since midnight today:

(DateTime.Now - DateTime.Today).TotalHours

Comparison operators

Operator Meaning
= same moment
<> different moments
< earlier than
<= earlier than or same
> later than
>= later than or same

All comparison operators return Boolean.

Examples

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

Result: True

DateTime.Parse('2026-11-23') = DateTime.Parse('2026-11-23 00:00:00')

Result: True. A date with no time part is midnight.

DateTime.Parse('2026-11-23') >= DateTime.Parse('2026-11-24')

Result: False

Model example

Employees hired within the last 90 days:

Employee.allInstances->select(e | e.HireDate >= DateTime.Today.AddDays(-90))

Notes

  • Subtracting two DateTimes always gives a TimeSpan. Use TotalDays, Days and the other TimeSpan operations to read it.
  • = compares the full date and time. To compare only the date, compare .Date on both sides, for example self.HireDate.Date = DateTime.Today.

See also