MultiValue OCONV/ICONV in .NET - Date Conversion
OCONV/ICONV commands are used extensively throughout our MultiValue systems. For many of us, they have become second nature, but to use them in other languages can cause problems. This article will explain how to mimic the OCONV/ICONV process in .NET for the Date Conversion codes.
OCONV a date
The following code will show you how to mimic the OCONV process for the Date conversions (ie: D4/ or D4-) within .NET.
The 1 st thing that needs to be done is convert the Internal MultiValue Date string into a .NET date. If you already have a .NET Date or DateTime variable, then you don't need to do this setup.
Dim integerValue as Integer If Not integer.TryParse(Value, integerValue) Then ' bad value, return Null Return string.Empty End If Dim internalDate as Date Dim zeroDate as New Date(1967, 12, 31) internalDate = System.DateTime.FromOADate(integerValue + zeroDate.ToOADate)
.NET has a nice little function built-in that converts their Date/time data type into an Integer (FromOADate). This allows use to generate your Internal MultiValue date really easy.
To create a .NET date, all that needs to be done is add the Zero Date value to the value passed in, and we suddenly have a .NET date.
Once we have our .NET date variable, we can start looking at the conversion code to decide what needs to be returned. If you want to recreate the OCONV function, then you will need to parse the conversion code into the Year Length and the delimiter your process is asking for the dates to place into. This then allows you to create a .ToString format.
Dim yearLength as Integer Dim offset as integer = 1 Dim delimit as String If Integer.TryParse(Conversion.SubString(offset,1) , yearLength) then ' Found Year Length. Offset = offset + 1 Else ' Not a year length, set the default length, and decide what to do next yearLength = 4 End if ' return the date delimiter delimit = Conversion.SubString(offset,1) ' Creates StringFormat Pattern dim stringFormat as string = "MM" & delimit &"dd" Select Case yearLength Case 1 stringFormat = stringFormat & delimit &"y" Case 2 stringFormat = stringFormat & delimit &"yy" Case 4 stringFormat = stringFormat & delimit &"yyyy" End Select ' display Formatted dim outputDate as string outputDate = internalDate.ToString(stringFormat)
Once you have the year length and the delimiter, you can figure out what the user wants to do with the date conversion. The following table give the most common Date conversions and how to mimic them in .NET
D |
internalDate.ToString("dd MMM yyyy") |
D0 |
internalDate.ToString("dd MMM") |
D2 |
internalDate.ToString("dd MMM yy") |
DD |
internalDate.Day.ToString |
DW |
internalDate.DayOfWeek
|
DWA |
InternalDate.ToString("dddd") |
DM |
internalDate.Month.ToString |
DWA |
InternalDate.ToString("mmmm") |
DY |
internalDate.Year.ToString |
D2Y |
internalDate.Month.ToString.SubString(3-yearLength,yearLength) |
D2/ |
internalDate.ToString("MM/dd/yy") |
D4/ |
internalDate.ToString("MM/dd/yyyy") |
D0/ |
internalDate.ToString("MM/dd") |
D4- |
internalDate.ToString("MM-dd-yyyy") |
ICONV a Date
Recreating the ICONV process on the Date Conversion code is a little easier.
Dim tempDate as Date
Dim internalDate as String
If Date.TryParse(value,tempDate) then
' Invalid Date
internalDate = String.Empty
else
Dim zeroDate as New Date(1967, 12, 31)
internalDate = (tempDate.ToOADate - zeroDate.ToOADate).ToString
End if