You can use formatDateTime in OCL to turn a DateTime value into a formatted string for display, derived attributes, and other text output.
Syntax
aDateTime.formatDateTime('format')
Replace aDateTime with the DateTime value you want to format. The format argument is a custom .NET date and time format string.
Format a date and time
Use a custom format string when you need a predictable text representation of a DateTime value.
self.FromTime.formatDateTime('yyyy-MM-dd HH:mm:ss')
This pattern produces text in the form year-month-day hour:minute:second, for example 2024-05-06 13:45:30.
| Format part | Meaning | Example output |
|---|---|---|
yyyy
|
Four-digit year | 2024
|
MM
|
Two-digit month | 05
|
dd
|
Two-digit day of the month | 06
|
HH
|
Hour in 24-hour notation | 13
|
mm
|
Two-digit minute | 45
|
ss
|
Two-digit second | 30
|
The literal characters in the format string are preserved. In yyyy-MM-dd HH:mm:ss, the hyphens, space, and colons appear in the result.
Display time only
For a time-entry field, format the stored DateTime value as hours and minutes:
self.FromTime.formatDateTime('HH:mm')
For a value representing 13:45, the result is 13:45. This pattern is used in the time-entry example in Documentation:Derived settable attributes.
Get the ISO 8601 week number
Pass 'week' to return the ISO 8601 week number of the year:
self.SomeDateTime.formatDateTime('week')
The result is the week number rather than a formatted date and time string.
Use formatDateTime in a derived attribute
A derived attribute can expose a DateTime value as display text while keeping the original value as DateTime.
if self.FromTime.notNull then
self.FromTime.formatDateTime('HH:mm')
else
String.nullValue
endif
In this example, the derived attribute returns formatted time when FromTime has a value. When it has no value, the expression returns String.nullValue instead of attempting to format it.
If users must enter text and you need to convert that text back to a DateTime value, see Documentation:Derived settable attributes and Documentation:OCLOperators strToDateTime.
