Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Two year digits with rollover support
Message
From
07/12/2011 12:53:05
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Two year digits with rollover support
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01530544
Message ID:
01530544
Views:
84
I have a somewhat equivalent function of the two year digits with rollover support. This also support a century optional parameter as well. Any improvement or suggestion is welcome. In this code, the 2nd and 3rd parameter are not defined optional but they are as in my framework this is called from a higher level forwarding method. So, as is, Optional might be added with 0 for the default value for those parameters:
        ' Convert two digits to year
        ' A number from 0 to 99 that specifies the year equal to and above which is the current century and below which is the next century.
        ' The default value for nYear is the last two digits of the current year plus 50 years - if the current year is 1998, nYear is 48, 
        ' the last two digits of 2048 (1998 + 50).
        ' Note that the rollover value only determines the century for a date entered without a century portion - an ambiguous date format 
        ' that is not recommended. 
        ' For example, if the current year is 1998 and nYear is the default (48), any date entered without a century portion and a year of 
        ' 48 or greater is considered to be in the current century (the 20th century). Any date entered without a century portion but with a
        ' year before 48 is considered to be in the next century (the 21st century). 
        ' expC1 Two digits
        ' expN1 Rollover year
        ' expN2 Century
        Public Function ConvertTwoDigitToYear(ByVal tcTwoDigit As String, ByVal tnRollover As Integer, _
         ByVal tnCentury As Integer) As Integer
            Dim lcTwoDigit As String = ""
            Dim lcYear As String = ""
            Dim ldDate As Date = Nothing
            Dim lnCentury As Integer = 0
            Dim lnRollover As Integer = 0
            Dim lnTwoDigit As Integer = 0
            Dim lnYear As Integer = 0

            ' Initialization
            lcTwoDigit = tcTwoDigit
            ldDate = Date.Now
            lnCentury = 21
            lnRollover = Val(Mid((ldDate.Year + 50).ToString, 3, 4))

            ' If we have a rollover value
            If tnRollover > 0 Then
                lnRollover = tnRollover
            End If

            ' If we have a century value
            If tnCentury > 0 Then
                lnCentury = tnCentury
            End If

            ' If this is numeric
            If oApp.IsNumeric(lcTwoDigit) Then

                ' If we have one or two digits
                If lcTwoDigit.Length = 1 Or lcTwoDigit.Length = 2 Then

                    ' Get it into a numeric
                    lnTwoDigit = Val(lcTwoDigit)

                    ' A century is always ahead
                    lnCentury = lnCentury - 1

                    ' If we are not in this century
                    If lnTwoDigit >= lnRollover Then
                        lnCentury = lnCentury - 1
                    End If

                    ' Get the year in character
                    lcYear = lnCentury.ToString + lcTwoDigit.PadLeft(2, "0")

                    ' Get the year in numeric
                    lnYear = Val(lcYear)

                Else

                    ' If we have four digits
                    If lcTwoDigit.Length = 4 Then

                        ' If we have four digits
                        lnYear = Val(lcTwoDigit)

                    End If

                End If

            End If

            Return lnYear
        End Function
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Reply
Map
View

Click here to load this message in the networking platform