Working with date values in AVR for .NET is a very frequently performed task. This article shows you how to convert and format dates in AVR for .NET.
.NET doesn't distinguish between date and time values like RPG does. .NET only uses the one data type, datetime, to represent both times and dates. AVR offers three date and time related data types:
- *Date
- *Time
- *TimeStamp
In all three cases, the underlying data type is really .NET's System.DateTime. In the case of *Date, the time values are set to minimal values and ignored. In the case of *Time, the date values are set to minimal values and ignored. If you look up DateTime in the Visual Studio help, you'll see all the members that the *Date, *Time, and *TimeStamp data types offer under the covers.
The following three pair of lines resolve to the identical data type and either AVR's or the .NET Framework's data type name may be used:
DclFld x Type( *Date )
DclFld x Type( DateTime )
DclFld x Type( *Time )
DclFld x Type( DateTime )
DclFld x Type( *TimeStamp )
DclFld x Type( DateTime )
The hands-down best way to work with numeric dates (despite the iSeries acquiring a date data type nearly ten years ago, there are still tons of dates stored as numeric values on the iSeries). Not only does converting these numeric dates to date data types help with formatting, but you'll soon see that .NET's powerful abilities to work with date and time values all but makes RPG operations such as SUBDUR and EXTRACT a thing of the past. (Don't get upset, AVR still supports those operations if you need them.
The general strategy for working with numeric dates is to convert them to date data types for formatting and to perform duration-based operations on them. Then, convert them back to numeric values to update changed numeric date values. If you're lucky enough to be using genuine date data types in your iSeries data, you're in chips--AVR reads them directly. All the formatting discussed here applies directly on those data types.
How to Convert a numeric or string value to date data type
The .NET Framework doesn't offer the ability to directly convert a numeric data type into a date data type. However, because all numeric types have ToString() readily available, it's just as easy to convert numerics as it is strings to date data types. However, to convert either, you need a mechanism that is smart enough to work with a variety of input formats.