You can use DateTime in MDriven expressions when you need to store, create, compare, or calculate a calendar date together with a time; this page is for modelers writing OCL expressions.
Choose the right type
Use a DateTime attribute when a user edits a date, including when the time is not significant. For a value that represents only a time of day, use TimeSpan instead. See Date vs Time for the type-selection guidance.
For example, an OrderDate attribute can hold 2025-12-10 14:30:00. A separate opening-time value such as 08:00 belongs in a TimeSpan value.
Get the current date and time
Use DateTime.Now to get the current date and time.
vCreatedAt = DateTime.Now
For example, if the expression is evaluated at 14:30 on 10 December 2025, DateTime.Now represents that date and time.
Create a specific date
Use DateTime.Create when you know the year, month, and day.
DateTime.Create(2025, 12, 10)
This expression creates a DateTime value for 10 December 2025. For details of the OCL Create operator, see Create.
You can also parse a date string:
DateTime.Parse('2020-01-01')
Use a string that includes a time when the time is required by the expression, for example:
DateTime.Parse('2025-01-01 00:00:00')
For string-to-DateTime conversion through the strToDateTime operator, see strToDateTime.
Work with the date or time part
A DateTime contains both a calendar date and a time. Remove the time part when you need to compare calendar dates without time precision.
self.OrderDate.date
The date operator returns the date part with the time set to 00:00:00. See date.
When you need the current time of day, use the time operator. See time. For comparisons against a time-of-day range, first use TimeOfDay and compare TimeSpan values; see inTimeRange.
Represent an empty non-nullable DateTime
A non-nullable DateTime cannot hold null. Use DateTime.Create(1, 1, 1) as the empty DateTime value when your model or expression needs a non-nullable value with no meaningful date.
vDateTimeValue = DateTime.Create(1, 1, 1)
You can test a non-nullable DateTime for this empty value by comparing it with the same expression:
vDateTimeValue = DateTime.Create(1, 1, 1)
The comparison returns true when vDateTimeValue has the empty non-nullable DateTime value. Treat this as a sentinel value: it is a DateTime value, not a nullable DateTime value.
Use DateTime with historical queries
allInstancesAtTime does not accept a DateTime directly. Convert the DateTime to the required integer timestamp with .timeToTimeStamp.
Complaint.allInstancesAtTime(DateTime.Parse('2025-01-01 00:00:00').timeToTimeStamp)
