You can choose the correct attribute type for dates, times of day, and date-time values when modeling data in MDriven Designer.
Choose the attribute type
A data type determines which values an attribute can hold and which operations you can perform on it. For date and time values, choose the type based on what the value means to the business.
| Requirement | Attribute type | Example | Notes |
|---|---|---|---|
| Store a calendar date, including when the user edits a date | DateTime | OrderDate = 2020-01-01
|
Use DateTime for a date even when the time of day is not meaningful. Use the date operation when you need to remove the time component in an expression.
|
| Store a date and time together | DateTime | CreatedAt = 2020-01-01 14:30
|
Use this for values such as a creation time or an appointment date and time. |
| Store or edit a time of day without a date | TimeSpan
|
OpeningTime = 08:30
|
Use TimeSpan when the date must not be part of the value, such as a daily opening time. |
Model common requirements
Date chosen by a user
Create a DateTime attribute when the user must enter a calendar date.
For example, model an OrderDate attribute as DateTime. If an order date should be compared as a calendar date rather than as an exact instant, use the date OCL operation:
Order.allinstances->first.OrderDate.dateThis returns the date portion of OrderDate, with the time set to 00:00:00. This is important when two values are intended to be compared by day but may have different times.
Time entered independently
Create a TimeSpan attribute when the user must enter only a time of day.
For example, use a TimeSpan attribute named OpeningTime for a store that opens at 08:30 each day. Do not use a DateTime attribute for this requirement if the date is not part of the business value.
Current date or current time
Use the appropriate OCL operation when you need a value based on the current system time:
datereturns the date part of the current system time, with the time set to midnight.timereturns the time part ofDateTime.Now.DateTime.Nowreturns both the current date and time.
For example, use date when an attribute or expression needs today's calendar date, and use DateTime.Now when you must also retain the current time.
Create and convert DateTime values
Use the DateTime operations described in DateTime to create a specific value:
DateTime.Parse('2020-01-01')
DateTime.Create(2020, 1, 1)When input can be invalid, use tryParse rather than assuming that conversion will succeed. It returns null for an invalid date or time string.
For example, an input value of '2020-01-01' can be converted to a DateTime. An invalid input such as 'not a date' results in null when using tryParse, so your logic can handle missing or invalid input explicitly. See also strToDate for string-to-date conversion.
Empty non-nullable DateTime values
A non-nullable DateTime can use DateTime.Create(1, 1, 1) as its empty value. You can compare a non-nullable DateTime attribute with that value to identify an empty value.
vDateTimeValue = DateTime.Create(1, 1, 1)See DateTime for the full behavior and examples.
Handle time zones deliberately
A DateTime can represent either a business-local date and time or a point that must be interpreted across time zones. Decide which meaning applies before adding conversion logic.
For example, an airport departure time is normally shown in the local time of the airport, while a webcast scheduled for viewers in different locations may require explicit time-zone information. Do not rely on the browser's current location to define the business meaning of a stored value. See Time zones and sanity–post for future reference for MDriven Turnkey handling and the related considerations.
Use the Calendar package for date-based analysis
If you need statistical analysis by days, months, years, weeks, or hours, link your model classes to the Calendar package rather than deriving the entire calendar structure repeatedly in expressions.
For example, an Account class with a CreateTime DateTime attribute can be linked to a Calendar Day. The related day provides access to its month and year for analysis.
See also
- Documentation:DateTime
- Documentation:OCLOperators date
- Documentation:OCLOperators time
- Documentation:OCLOperators tryParse
- Documentation:Time zones and sanity–post for future reference
[[Category:Attributes and data types [Beginner]]]
