OCLOperators tryParse
No edit summary
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
The tryParse operator converts a string representation of a date and time to its DateTime equivalent. Null is returned  if the string is not a valid date and or time.
The tryParse operator converts a string representation of a date and time to its DateTime equivalent. Null is returned  if the string is an invalid date and/or time.


The string to be parsed can be in any of the following forms,
The string to be parsed can be in any of the following forms:


* A string with a date and a time component.
* A string with a date and a time component.
{| class="wikitable"
{| class="wikitable"
|+
!OCL
!OCL
!Result
!Result
|-
|-
|'08/18/2018 07:22:16'.tryParse(<nowiki>''</nowiki>)
|'08/18/2018 07:22:16'.tryParse(<nowiki>''</nowiki>)
|18/08/2018 07:22:16
|8/18/2018 07:22:16 AM
|}
|}
* A string with a date but no time component. If the time component is absent, the method assumes 12:00 midnight. If the date component has a two-digit year, it is converted to a year based on the Calendar.
*A string with a date but no time component. If the time component is absent, the method assumes 12:00 midnight. If the date component has a two-digit year, it is converted to a year based on the Calendar.
* A string with a date component that includes only the month and the year but no day component. The method assumes the first day of the month.
{| class="wikitable"
* A string with a date component that includes only the month and the day but no year component. The method assumes the current year.
!OCL
* A string with a time but no date component.
!Result
|-
| <nowiki>'08/18/2018'.tryParse(''</nowiki>)
| 8/18/2018 12:00:00 AM
|}
*A string with a date component that includes only the month and the year but no day component. The method assumes the first day of the month.
{| class="wikitable"
!OCL
!Result
|-
|<nowiki>'8/2018'.tryParse(''</nowiki>)
| 8/1/2018 12:00:00 AM
|}
*A string with a date component that includes only the month and the day but no year component. The method assumes the current year.
{| class="wikitable"
!OCL
!Result
|-
| <nowiki>'8/18'.tryParse(''</nowiki>)
|8/18/2024 12:00:00 AM
|}
 
* A string with a time but no date component. The method assumes the current date.
 
{| class="wikitable"
!OCL
!Result
|-
| '07:22:16'.tryParse(<nowiki>''</nowiki>)
|8/18/2024 7:22:16 AM
|}
 
* A string with a time component that includes only the hour and an AM/PM designator, with no date component. The method assumes the current date and a time with no minutes and no seconds.
* A string with a time component that includes only the hour and an AM/PM designator, with no date component. The method assumes the current date and a time with no minutes and no seconds.
{| class="wikitable"
!OCL
!Result
|-
| <nowiki>'7 PM'.tryParse(''</nowiki>)
|8/18/2024 7:00:00 PM
|}
* A string that includes time zone information and conforms to ISO 8601. In the following examples, the first string designates Coordinated Universal Time (UTC), and the second designates the time in a time zone that's seven hours earlier than UTC: "2008-11-01T19:35:00.0000000Z" "2008-11-01T19:35:00.0000000-07:00"
* A string that includes time zone information and conforms to ISO 8601. In the following examples, the first string designates Coordinated Universal Time (UTC), and the second designates the time in a time zone that's seven hours earlier than UTC: "2008-11-01T19:35:00.0000000Z" "2008-11-01T19:35:00.0000000-07:00"
* A string that includes the GMT designator and conforms to the <nowiki>RFC 1123</nowiki> time format; for example:  "Sat, 01 Nov 2008 19:35:00 GMT"
 
* A string that includes the date and time along with time zone offset information; for example:  "03/01/2009 05:42:00 -5:00"
{| class="wikitable"
!OCL
!Result
|-
| '2018-08-18T07:22:16.0000000Z'.tryParse(<nowiki>''</nowiki>)
|8/18/2018 10:22: 16 AM
|-
|'2018-08-18T07:22:16.0000000-07:00'.tryParse(<nowiki>''</nowiki>)
|8/18/2018 5:22:16 PM
|}
 
* A string that includes the GMT designator and conforms to the <nowiki>RFC 1123</nowiki> time format - for example:  "Sat, 01 Nov 2008 19:35:00 GMT"
{| class="wikitable"
!OCL
!Result
|-
| 'Sat, 18 Aug 2018 07:22:16 GMT'.tryParse(<nowiki>''</nowiki>)
|8/18/2018 10:22:16 AM
|}
 
* A string that includes the date and time along with time zone offset information - for example:  "03/01/2009 05:42:00 -5:00"
 
{| class="wikitable"
!OCL
!Result
|-
| '08/18/2018 07:22:16 -5:00'.tryParse(<nowiki>''</nowiki>)
|8/18/2018 3:22:16 PM
|}

Latest revision as of 05:20, 19 August 2024

The tryParse operator converts a string representation of a date and time to its DateTime equivalent. Null is returned if the string is an invalid date and/or time.

The string to be parsed can be in any of the following forms:

  • A string with a date and a time component.
OCL Result
'08/18/2018 07:22:16'.tryParse('') 8/18/2018 07:22:16 AM
  • A string with a date but no time component. If the time component is absent, the method assumes 12:00 midnight. If the date component has a two-digit year, it is converted to a year based on the Calendar.
OCL Result
'08/18/2018'.tryParse('') 8/18/2018 12:00:00 AM
  • A string with a date component that includes only the month and the year but no day component. The method assumes the first day of the month.
OCL Result
'8/2018'.tryParse('') 8/1/2018 12:00:00 AM
  • A string with a date component that includes only the month and the day but no year component. The method assumes the current year.
OCL Result
'8/18'.tryParse('') 8/18/2024 12:00:00 AM
  • A string with a time but no date component. The method assumes the current date.
OCL Result
'07:22:16'.tryParse('') 8/18/2024 7:22:16 AM
  • A string with a time component that includes only the hour and an AM/PM designator, with no date component. The method assumes the current date and a time with no minutes and no seconds.
OCL Result
'7 PM'.tryParse('') 8/18/2024 7:00:00 PM
  • A string that includes time zone information and conforms to ISO 8601. In the following examples, the first string designates Coordinated Universal Time (UTC), and the second designates the time in a time zone that's seven hours earlier than UTC: "2008-11-01T19:35:00.0000000Z" "2008-11-01T19:35:00.0000000-07:00"
OCL Result
'2018-08-18T07:22:16.0000000Z'.tryParse('') 8/18/2018 10:22: 16 AM
'2018-08-18T07:22:16.0000000-07:00'.tryParse('') 8/18/2018 5:22:16 PM
  • A string that includes the GMT designator and conforms to the RFC 1123 time format - for example: "Sat, 01 Nov 2008 19:35:00 GMT"
OCL Result
'Sat, 18 Aug 2018 07:22:16 GMT'.tryParse('') 8/18/2018 10:22:16 AM
  • A string that includes the date and time along with time zone offset information - for example: "03/01/2009 05:42:00 -5:00"
OCL Result
'08/18/2018 07:22:16 -5:00'.tryParse('') 8/18/2018 3:22:16 PM
This page was edited 32 days ago on 08/19/2024. What links here