The DataTime object's ParseExact() method provides the functionality that you need. ParseExact takes three arguments:
- the string value of the date to convert.
- the format string to use to convert the date.
- optionally, an object that provides culture-specific formatting information. For basic formatting, you don't need to provide a culture-specific formatting object.
Here is a minimal example using ParseExact. In this case,
DclFld DateVal Type( DateTime )
DateVal = DateTime.ParseExact( DateValue, FormatString, *Nothing )
For example, assume you had a yyyymmdd numeric date field named MyDate with the value 20050415 in it. To convert this value to a date data type, you could use:
DclFld Date Type( *Date )
Date = DateTime.ParseExact( MyDate.ToString( "00000000" ), "yyyyMMdd", *Nothing )
In the case above, the ToString() method uses the custom numeric formatting string of "00000000" to ensure that no leading zeros are dropped.
If the potential exists for you to need to convert a numeric date that might not be valid, you could test for an invalid date with a try/catch, like this:
DclFld Date Type( *Date )
Try
Date = DateTime.ParseExact( MyDate.ToString( "00000000" ), "yyyyMMdd", *Nothing )
Catch Err Type( System.Exception )
// Handle date conversion failure here.
EndTry
To provide the most flexible date conversions, consider using the function below. It uses regular expression pattern matching to determine the format of the date passed in. The formats it supports are documented in the function:
BegFunc ConvertFromString Type( DateTime )
DclSrParm DateValue Type( *String )
// Two digit year century inference:
// .NET date formatting assumes that dates between 1/1/00 and
// 12/31/29 are twenty-first century dates (that is, the years
// are assumed to be 2000 to 2029). Dates between 1/1/30
// and 12/31/99 are assumed to be twentieth century dates
// (that is, the years are assumed to be 1930 to 1999).
// mmddyy
DclFld dddddd Type( *String ) Inz( "^\d{6}$" )
// yyyyddmm
DclFld dddddddd Type( *String ) Inz( "^\d{8}$" )
// mm-dd-yy
DclFld dd_dd_dd Type( *String ) Inz( "^\d{2}-\d{2}-\d{2}$" )
// mm-dd-yyyy
DclFld dd_dd_dddd Type( *String ) Inz( "^\d{2}-\d{2}-\d{4}$" )
// yyyy-mm-dd
DclFld dddd_dd_dd Type( *String ) Inz( "^\d{4}-\d{2}-\d{2}$" )
DclFld FormatString Type( *String ) Inz( "" )
DclFld Result Type( DateTime )
If ( DateValue.IndexOf( "/" ) > -1 )
DateValue = DateValue.Replace( "/", "-" )
EndIf
If ( RegEx.Match( DateValue, dddddd ).Success )
FormatString = "MMddyy"
ElseIf ( RegEx.Match( DateValue, dddddddd ).Success )
FormatString = "yyyyMMdd"
ElseIf ( RegEx.Match( DateValue, dd_dd_dd ).Success )
FormatString = "MM-dd-yy"
ElseIf ( RegEx.Match( DateValue, dd_dd_dddd ).Success )
FormatString = "MM-dd-yyyy"
Elseif ( RegEx.Match( DateValue, dddd_dd_dd ).Success )
FormatString = "yyyy-MM-dd"
EndIf
Try
Result = DateTime.ParseExact( DateValue, FormatString, *Nothing )
Catch Err Type( System.Exception )
Throw Err
EndTry
LeaveSr Value( Result )
EndFunc
Once a numeric value is converted to a date data type, you have many class members at your disposal. For example, you'll have instance access to properties such as:
- Day
- Month
- Year
- WeekDay
- DayOfWeek
- DayOfYear
and access to methods such as:
- AddDays
- AddHours
- AddMinutes
- AddMonths
- IsLeapYear
- Subtract
- ToString (with its powerful date formatting capabilities)
As you can see, to work with dates in AVR for .NET, you really need to convert your numerically stored dates into true date data types. Please understand that we aren't advocating the wholesale conversion of your database from numeric data types to date data types. What we're advocating is that you convert your numeric date types to date data types for program processing. While it might be nice to convert your database date data types, it isn't at all necessary.
Once you've learned to convert a numeric date to a true date data type, you might want to make changes to that date's value and write that new numeric date representation back to disk. The next section shows how to convert from date data types back to numeric date data types.