B4J Question [BANano] [SOLVED] Incorrect Math / NumberFormat Result

Mashiane

Expert
Licensed User
Longtime User
Hi There

B4X:
Sub GetDOBFromRSAID(IDNumber As String) As String
    IDNumber = PadRight(IDNumber, 13, "0")
    Dim yy As Int = IDNumber.SubString2(0, 2)
    Dim mm As Int = IDNumber.SubString2(2, 4)
    Dim dd As Int = IDNumber.SubString2(4, 6)
    Dim lNow As Long = DateTime.Now
    Dim currentYear As Int = DateTime.GetYear(lNow)
    Dim currentCentury As Int = (currentYear / 100) * 100
    Dim fullYear As Int = currentCentury + yy
    If fullYear > currentYear Then
        fullYear = fullYear - 100
    End If
    Return NumberFormat(fullYear,4,0) & "-" & NumberFormat(mm,2,0) & "-" & NumberFormat(dd,2,0)
End Sub

This is returning an error when using a 13 digit number "7304150000000", its supposed to be 1973-04-15.

B4X:
202,473-04-15]
 

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Dim currentCentury As Int = (currentYear / 100) * 100
To
B4X:
Dim currentCentury As Int = Floor(currentYear / 100)
currentCentury = currentCentury * 100
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Working Solution

B4X:
Sub ExtractDateOfBirthFromRSAID(IDNumber As String) As String
    If IDNumber.Length <> 13 Then
        IDNumber = PadRight(IDNumber, 13, "0")
    End If
    Dim yy As Int = IDNumber.SubString2(0, 2)
    Dim mm As Int = IDNumber.SubString2(2, 4)
    Dim dd As Int = IDNumber.SubString2(4, 6)
    Dim lNow As Long = DateTime.Now
    Dim currentYear As Int = DateTime.GetYear(lNow)
    Dim currentCentury As Int = Floor(currentYear / 100)
    currentCentury = currentCentury * 100
    Dim fullYear As Int = BANano.parseInt(currentCentury) + BANano.parseInt(yy)
    If fullYear > currentYear Then
        fullYear = fullYear - 100
    End If
    Return NumberFormat2(fullYear,4,0,0,False) & "-" & NumberFormat2(mm,2,0,0,False) & "-" & NumberFormat2(dd,2,0,0,False)
End Sub
 
Upvote 0
Top