B4R Tutorial DATE to Uint / Unit to Date Converter

I using this code, Because Board has a problem for records size capacity of epprom.
B4X:
'For date >> 2015/ 08 / 04 (y/m/d)
Log("Date number:",DateTime.DateToUint(15,8,4)))
'returned >>
'Date number: 5727
'(2byte / uint)

Log("Date String:",DateTime.UintToDate(5727))
'returned >>
'Date string: 150804
'(string)
2016/08/19 =10byte
20160819 =8byte
mycode = 2byte (uint)


This min date start 2000 years.
have nice weekend.

B4X:
Public Sub DateToUint(YY As UInt,MM As UInt,DD As UInt) As UInt
    Dim y As UInt
    If YY>2000 Then y=YY-2000 Else y=YY
    Dim total As UInt
    total=y*365+Floor(y/4)
    For i=1 To MM
        total=total+AYgunu(YY,i)
    Next
    Return total+DD
End Sub


Public Sub UintToDate(sayi As UInt) As String

    Dim YY,MM,DD,net,Tday As UInt
    YY=(sayi/365.25)'yıl hesaplandı
    net=sayi-(YY*365+Floor(YY/4))'kalan
    Tday=0
    MM=1
    Do Until (Tday+AYgunu(YY,MM)>net Or MM>12)
        Tday=Tday+AYgunu(YY,MM)
        MM=MM+1
    Loop
    MM=MM-1
    DD=net-Tday

    Return StdDateToString(YY,MM,DD)
End Sub

Public Sub StdDateToString(YY As UInt,MM As UInt,DD As UInt) As String

    Dim s As String
    s=""
    If YY<10 Then
            s=JoinStrings(Array As String("0", YY))
        Else
            s=JoinStrings(Array As String(s, YY))
    End If

    If MM<10 Then
  
            s=JoinStrings(Array As String(s,JoinStrings(Array As String("0", MM))))
        Else
      
            s=JoinStrings(Array As String(s,MM))
    End If
    If DD<10 Then
            s=JoinStrings(Array As String(s,JoinStrings(Array As String("0", DD))))
        Else
            s=JoinStrings(Array As String(s, DD))
    End If
Return s
End Sub

'this is calculate days for each year and months
Sub AYgunu(YY As UInt, MM As UInt) As UInt
    Select MM
        Case 1,3,5,7,8,10,12
            Return 31
        Case 4,6,9,11
            Return 31
        Case 2
            If (YY Mod 4)=0 Then Return 29 Else Return 28
        Case Else
            Return 0
    End Select
End Sub
 
Last edited:
Top