🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators inTimeRange
This page was created by Stephanie on 2025-01-08. Last edited by Wikiadmin on 2026-07-29.

You can use inTimeRange in OCL when you need to test whether a time of day falls within a specified time range.

Purpose

inTimeRange is a Boolean operator: it returns true or false. Use it to compare a TimeSpan value, such as the time-of-day portion of a DateTime, with two TimeSpan range values.

For example, an order date such as 2026-03-09 14:30 is a DateTime. Its TimeOfDay value is 14:30:00, which is a TimeSpan and can be supplied to inTimeRange.

Syntax

self.[DateTimeProperty].TimeOfDay.inTimeRange(TimeSpan.FromHours(X), TimeSpan.FromHours(Y))
Part Meaning
self.[DateTimeProperty] The DateTime property that you want to test.
.TimeOfDay Extracts the duration since midnight as a TimeSpan. This makes the comparison independent of the calendar date.
TimeSpan.FromHours(X) Creates a TimeSpan range boundary by calling the .NET TimeSpan.FromHours method through the CLR.
inTimeRange(...) Tests the extracted TimeSpan against the two supplied range boundaries and returns a Boolean value.

Use a DateTime property's time of day

Follow these steps when the property you have is a DateTime:

  1. Start with the DateTime property.
  2. Call TimeOfDay to obtain a TimeSpan.
  3. Define both range boundaries as TimeSpan values.
  4. Use the Boolean result in an expression or assign it to a variable.
vResult := self.OrderDate.TimeOfDay.inTimeRange(TimeSpan.FromHours(0), TimeSpan.FromHours(0))

In this example:

  • self.OrderDate is a DateTime.
  • self.OrderDate.TimeOfDay is the corresponding TimeSpan.
  • Both boundaries are created as TimeSpan values.
  • vResult receives the Boolean result.

The := operator assigns the result so that later OCL logic or a user-interface expression can use vResult.

Avoid DateTime and TimeSpan type mismatches

Do not compare a full DateTime directly with TimeSpan boundaries. They are different types. A comparison such as the following supplies a TimeSpan to inTimeRange because TimeOfDay performs the required extraction:

self.OrderDate.TimeOfDay.inTimeRange(TimeSpan.FromHours(8), TimeSpan.FromHours(17))

This expression tests the order's time of day using boundaries representing 8 and 17 hours after midnight. It does not compare the date portion of OrderDate.

Notes

  • The operator is CLR-bound; its time comparisons use standard .NET time values, including ticks and milliseconds.
  • The result is always Boolean. For general logical composition of Boolean expressions, see OCL Boolean Operators.
  • Confirm the boundary behavior for equal start and end values and for ranges that cross midnight before using those cases in business rules.

See also