This article shows how to convert an AVR for .NET date data type into a numeric value. 

Assume you have a date value like this:

DclFld Date Type(*Date) 

// Set the field to today's date.
Date = DateTime.Now

To convert the date field into one eight-digit numeric field in the format yyyymmdd, do this:

DclFld yyyymmdd Type(*Integer4) 
yyyymmdd = Date.ToString("yyyyMMdd") 

To get the date field into two numeric fields, one two-digit field for the leading two digits of the year and the other six-digit field for yymmdd, do this:

DclFld yy      Type(*Integer4) 
DclFld yymmdd  Type(*Integer4) 
  
// Get the leading two digits of the year.
yy = Date.ToString("yyyy").SubString(0, 2) 
// Get yymmdd numeric value.
yymmdd  = Date.ToString("yyMMdd")

When updating old six-digit date apps to be Y2K compliant, some shops shoehorned in a fix where they separately store the leading two digits of the year in its own field. This value is loosely called the "century" by these shops, but it really isn't the century, it's just the leading two digits of the 4-digit year. The code above is what those shops could use to convert a date data type back into the numeric values they need to store on their IBM i.

These techniques work by using .NET's custom date formatting strings to specify parts of the dates to extract. These custom date formatting strings are case-sensitive. In this case, the formatting strings used are:

  • yyyy = the 4-digit year
  • MM = the 2-digit month
  • dd = the 2-digit day

.NET doesn't provide a direct way to access the leading two digits of the year. Thus, this code:

yy = Date.ToString( "yyyy" ).SubString(0, 2) 

is used to fetch that value by plucking off the first two digits of the four digit year.