Public Shared Function CompeleteDateStr(DateStr As String) As String
    'MBS 96-09-25: High Caliber DataEntry
    Dim DateVal = CType(DateStr, Integer)
    If DateVal < 1 Then Return ""
    If DateVal < maxDay Then 'DayOnly
        Return MakeJalaliDate(curYear, curMonth, DateVal, _4DigitYear)
    ElseIf DateVal > maxDay Then
        Dim YearPiece? As Short = Nothing, MonthPiece? As Byte = Nothing, DayPiece As Byte
        Select Case DateVal
            Case Is < 100
                MonthPiece = (DateVal \ 10) Mod 10
                DayPiece = DateVal Mod 10
                If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, curYear) Then Return MakeJalaliDate(curYear, MonthPiece, DayPiece, _4DigitYear)
            Case Is < 1000
                MonthPiece = DateVal \ 10
                DayPiece = DateVal Mod 10
                If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, curYear) Then Return MakeJalaliDate(curYear, MonthPiece, DayPiece, _4DigitYear)
                MonthPiece = (DateVal \ 100) Mod 10
                DayPiece = DateVal Mod 100
                If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, curYear) Then Return MakeJalaliDate(curYear, MonthPiece, DayPiece, _4DigitYear)
                DayPiece = DateVal \ 10
                MonthPiece = DateVal Mod 10
                If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, curYear) Then Return MakeJalaliDate(curYear, MonthPiece, DayPiece, _4DigitYear)
                DayPiece = (DateVal \ 100) Mod 10
                MonthPiece = DateVal Mod 100
                If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, curYear) Then Return MakeJalaliDate(curYear, MonthPiece, DayPiece, _4DigitYear)
            Case Is < 10000
                MonthPiece = DateVal \ 100
                DayPiece = DateVal Mod 100
                If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, curYear) Then Return MakeJalaliDate(curYear, MonthPiece, DayPiece, _4DigitYear)
                MonthPiece = DateVal Mod 100
                DayPiece = DateVal \ 100
                If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, curYear) Then Return MakeJalaliDate(curYear, MonthPiece, DayPiece, _4DigitYear)
                DayPiece = DateVal \ 100
                MonthPiece = DateVal Mod 100
                If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, curYear) Then Return MakeJalaliDate(curYear, MonthPiece, DayPiece, _4DigitYear)
                DayPiece = DateVal Mod 100
                MonthPiece = DateVal \ 100
                If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, curYear) Then Return MakeJalaliDate(curYear, MonthPiece, DayPiece, _4DigitYear)
                MonthPiece = (DateVal \ 10) Mod 10
                YearPiece = DateVal \ 100
                If IsValidMonth(MonthPiece) Then
                    DayPiece = DateVal Mod 10
                    If IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece, _4DigitYear)
                Else
                    MonthPiece = (DateVal \ 100) Mod 10
                    YearPiece = DateVal Mod 100
                    DayPiece = DateVal \ 1000
                    If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece, _4DigitYear)
                End If
            Case Is < 100000
                Dim DaySize As Byte = 2
                MonthPiece = (DateVal Mod 1000) \ 10
                If Not IsValidMonth(MonthPiece) Then
                    MonthPiece = (DateVal \ 100) Mod 10
                    DaySize = 1
                    If Not IsValidMonth(MonthPiece) Then Return ""
                End If
                YearPiece = DateVal \ 1000
                DayPiece = DateVal Mod 10 ^ DaySize
                If IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece)
                YearPiece = DateVal Mod 100
                DayPiece = DateVal \ (10000 \ (10 ^ DaySize))
                If IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece)
            Case Is < 1000000
                MonthPiece = (DateVal Mod 10000) \ 100
                If IsValidMonth(MonthPiece) Then
                    YearPiece = DateVal \ 10000
                    DayPiece = DateVal Mod 100
                    If IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece, _4DigitYear)
                    YearPiece = DateVal Mod 100
                    DayPiece = DateVal \ 10000
                    If IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece, _4DigitYear)
                End If
                YearPiece = DateVal \ 100
                If YearPiece > 1300 AndAlso YearPiece < 1500 Then
                    MonthPiece = (DateVal \ 10) Mod 10
                    DayPiece = DateVal Mod 10
                    If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece, _4DigitYear)
                End If
                YearPiece = DateVal Mod 10000
                If YearPiece > 1300 AndAlso YearPiece < 1400 Then
                    DayPiece = DateVal \ 100000
                    MonthPiece = (DateVal \ 10000) Mod 10
                    If IsValidMonth(MonthPiece) AndAlso IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece + If(_4DigitYear, 0, -1300), MonthPiece, DayPiece, _4DigitYear)
                End If
            Case Is < 10000000
                Dim DaySize As Byte = 2
                MonthPiece = (DateVal Mod 1000) \ 10
                If Not IsValidMonth(MonthPiece) Then
                    MonthPiece = (DateVal \ 100) Mod 10
                    DaySize = 2
                    If Not IsValidMonth(MonthPiece) Then Return ""
                End If
                YearPiece = DateVal \ 1000
                DayPiece = DateVal Mod 10 ^ DaySize
                If IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece)
                YearPiece = DateVal Mod 10000
                DayPiece = DateVal \ (100000 * (10 ^ DaySize))
                If IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece)
            Case Is < 100000000
                MonthPiece = (DateVal Mod 10000) \ 100 '13961229
                If IsValidMonth(MonthPiece) Then
                    YearPiece = DateVal \ 10000
                    DayPiece = DateVal Mod 100
                    If IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece, _4DigitYear)
                    YearPiece = DateVal Mod 10000
                    DayPiece = DateVal \ 1000000
                    If IsValidDay(DayPiece, MonthPiece, YearPiece) Then Return MakeJalaliDate(YearPiece, MonthPiece, DayPiece, _4DigitYear)
                End If
        End Select
        Return ""
    End If
End Function

this code is so wrong in so many different levels

By NoobProger, 2017-12-20 12:37:08