B4R Tutorial sub for Date Time adding

Hello;
I needed date time functions; And some write codes.
it is first sharing with B4R

use this variables for date &time
Public Time_HH As Int
Public Time_MM As Int
Public Time_SS As Int
Public Date_DD As Int
Public Date_MM As Int
Public Date_YY As Int

  • AddSecond(Sec As Int) As Int
    • for adding seconds
  • AddMinute(minut As Int) As Int
    • for adding minutes
  • AddHour(Hour As Int) As Int
    • for adding hours
  • AddDay(Day As Int) As Int
    • for adding days
  • AddMonth(Month As Int) As Int
    • for adding months
  • AddYear(Year As Int) As Int
    • for adding years
  • AddDateTime(Year As Int,Month As Int,Day As Int,Hour As Int,Mint As Int, Secd As Int)
    • for adding all times & dates
  • GetDays(Mount As Int,Year As Int) As Int
    • for get days vaules for special month/year
  • ReadDate(data() As Byte)
    • for convert from bytes datetime values to golabal date&time variables
Connection: close
Date: Fri, 02 Sep 2016 09:43:50 GMT
Server: Microsoft-IIS/6.0
-1-1
X-Powered-By: ASP.NET
Content-Length: 30
Content-Type: text/html
Set-Cookie: ASPSESSIONIDQCDCBTRB
-1-1
=FJMHLEKAJGFILHJHJLKCDOCN; path=/
Cache-control: private
<DT>02.09.2016 12:43:50</DT>
6184
Date time>>02.09.2016 12:43:50
12:43:50 2016/9/2
AddSecond(78)
12:45:8 2016/9/2
AddMinute(83)
14:8:8 2016/9/2
AddHour(33)
23:8:8 2016/9/3
AddDay(46)
23:8:8 2016/10/18
AddMonth(18)
23:8:8 2018/4/18
AddYear(21)
23:8:8 2039/4/18
Disconnected


B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial

   Private wifi As ESP8266WiFi
   Private astream As AsyncStreams
   Private socket As WiFiSocket

   Public Time_HH As Int
   Public Time_MM As Int
   Public Time_SS As Int
   Public Date_DD As Int
   Public Date_MM As Int
   Public Date_YY As Int

End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
  '**************** ASP file**** datetime.asp *****************
    ' you can read on web page form internet
' asp file code:
'<%=now()%>

ReadDate("01.09.2016 16:49:45".GetBytes)
End Sub



Sub ReadDate(data() As Byte)

    Dim bc As ByteConverter
    Log("Date time>>",data) ' Date time>>01.09.2016 16:49:45
    'Log("Gün:", x(0))
     'step 1: get the correct substring
   'step 2: convert it to string as array of bytes cannot be directly converted to numbers
   Date_DD=bc.StringFromBytes(bc.SubString2(data, 0, 2))
   Date_MM=bc.StringFromBytes(bc.SubString2(data, 3, 5))
   Date_YY=bc.StringFromBytes(bc.SubString2(data, 6, 10))

   Time_HH=bc.StringFromBytes(bc.SubString2(data, 11, 13))
   Time_MM=bc.StringFromBytes(bc.SubString2(data, 14, 16))
   Time_SS=bc.StringFromBytes(bc.SubString2(data, 17, 19))
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Time_SS=AddSecond(78)
   Log("AddSecond(78)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Time_MM=AddMinute(83)
   Log("AddMinute(83)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Time_HH=AddHour(33)
   Log("AddHour(33)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Date_DD=AddDay(46)
   Log("AddDay(46)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Date_MM=AddMonth(18)
   Log("AddMonth(18)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Date_YY=AddYear(21)
   Log("AddYear(21)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)

End Sub

Sub AddDateTime(Year As Int,Month As Int,Day As Int,Hour As Int,Mint As Int, Secd As Int)
    If Secd <>0 Then
        Time_SS=AddSecond(Secd)
    End If
    If Mint<>0 Then
        Time_MM=AddMinute(Mint)
    End If
    If Hour<>0 Then
        Time_HH=AddHour(Hour)
    End If
    If Day<>0 Then
        Date_DD=AddDay(Day)
    End If
    If Month<>0 Then
        Date_MM=AddMonth(Month)
    End If
    If Year<>0 Then
        Date_YY=AddYear(Year)
    End If

End Sub

Sub AddSecond(Sec As Int) As Int
    Dim Time_SSx As Int
    Time_SSx=Time_SS+Sec
    If Time_SSx>59 Then
        Time_MM= AddMinute((Time_SSx-(Time_SSx Mod 60))/60)
        Return (Time_SSx Mod 60)
    Else if Time_SSx<0 Then
        Time_MM= AddMinute((Time_SSx-(Time_SSx Mod 60))/60-1)
        Return (Time_SSx Mod 60)+60
    Else
        Return Time_SSx
    End If
End Sub
Sub AddMinute(minut As Int) As Int
    Dim Time_MMx As Int
    Time_MMx=Time_MM+minut
    If Time_MMx>59 Then
        Time_HH= AddHour((Time_MMx-(Time_MMx Mod 60))/60)
        Return (Time_MMx Mod 60)
    Else if Time_MMx<0 Then
        Time_HH= AddHour((Time_MMx-(Time_MMx Mod 60))/60-1)
        Return (Time_MMx Mod 60)+60
    Else
        Return Time_MMx
    End If
End Sub
Sub AddHour(Hour As Int) As Int
    Dim Time_HHx As Int
    Time_HHx=Time_HH+Hour
    If Time_HHx>23 Then
        Date_DD= AddDay((Time_HHx-(Time_HHx Mod 24))/24)
        Return (Time_HHx Mod 24)
    Else if Time_HHx<0 Then
        Date_DD= AddDay((Time_HHx-(Time_HHx Mod 24))/24-1)
        Return (Time_HHx Mod 24)+24
    Else
        Return Time_HHx
    End If
End Sub

Sub GetDays(Mount As Int,Year As Int) As Int
    Dim limit As Int=0
    Dim mnt As Int=Mount
    Dim yr As Int
    yr=Year
    If Mount<1 Then
        mnt=Mount+12
        yr=yr-1
        If mnt<1 Then
            mnt=mnt+12
            yr=yr-1
            If mnt<1 Then
                mnt=mnt+12
                yr=yr-1
            End If
        End If
    End If
  
  
    Select mnt
        Case 1,3,5,7,8,10,12
            limit=31
        Case 4,6,9,11
            limit=30
        Case 2
            If (yr Mod 4)=0 Then limit=29 Else limit=28
    End Select
  
    Return limit

End Sub
Sub AddDay(day As Int) As Int
    Dim mnt As Int=0
    Dim lmt As Int=0
    mnt=Date_MM-1
    If mnt<1 Then mnt=12
    Dim limit As Int
    limit=GetDays(Date_MM,Date_YY)
    Dim Date_DDx As Int
    Date_DDx=Date_DD+day
    If  Date_DDx>limit Then
     Date_MM=AddMonth((Date_DDx-(Date_DDx Mod limit))/limit)
     Return (Date_DDx Mod limit)
    Else If Date_DDx<0 Then
        Dim i As Int=1
        Dim Date_MMx As Int=0
        Do Until (Date_DDx+GetDays(Date_MM-i,Date_YY))>0
            Date_DDx=Date_DDx+GetDays(Date_MM-i,Date_YY)
            Date_MMx=Date_MMx-1
            i=i+1
        Loop
        If Date_DDx<1 Then
            Date_MM=AddMonth(Date_MMx-1)
            Return (Date_DDx+GetDays(Date_MM,Date_YY))
        Else
            Log("else...PROBLEMMMMMMMM")
            Return 1
        End If  
    Else
      
        Return Date_DDx
    End If
End Sub

Sub AddMonth(Month As Int) As Int
    Dim Date_MMx As Int
    Date_MMx=Date_MM+Month
    If Date_MMx>12 Then
        Date_YY= AddYear((Date_MMx-(Date_MMx Mod 12))/12)
        Return (Date_MMx Mod 12)
    Else if Date_MMx<0 Then
        Date_DD= AddDay((Date_MMx-(Date_MMx Mod 12))/12-1)
        Return (Date_MMx Mod 12)+12
    Else
        Return Date_MMx
    End If
End Sub

Sub AddYear(Year As Int) As Int
    Return (Date_YY+Year)
End Sub
 
Last edited:

Tayfur

Well-Known Member
Licensed User
Longtime User
UPDATED:

Now; code works
Ability to work with positive and negative numbers


Hello;
I needed date time functions; And some write codes.
it is first sharing with B4R

use this variables for date &time
Public Time_HH As Int
Public Time_MM As Int
Public Time_SS As Int
Public Date_DD As Int
Public Date_MM As Int
Public Date_YY As Int

  • AddSecond(Sec As Int) As Int
    • for adding seconds
  • AddMinute(minut As Int) As Int
    • for adding minutes
  • AddHour(Hour As Int) As Int
    • for adding hours
  • AddDay(Day As Int) As Int
    • for adding days
  • AddMonth(Month As Int) As Int
    • for adding months
  • AddYear(Year As Int) As Int
    • for adding years
  • AddDateTime(Year As Int,Month As Int,Day As Int,Hour As Int,Mint As Int, Secd As Int)
    • for adding all times & dates
  • GetDays(Mount As Int,Year As Int) As Int
    • for get days vaules for special month/year
  • ReadDate(data() As Byte)
    • for convert from bytes datetime values to golabal date&time variables
Connection: close
Date: Fri, 02 Sep 2016 09:43:50 GMT
Server: Microsoft-IIS/6.0
-1-1
X-Powered-By: ASP.NET
Content-Length: 30
Content-Type: text/html
Set-Cookie: ASPSESSIONIDQCDCBTRB
-1-1
=FJMHLEKAJGFILHJHJLKCDOCN; path=/
Cache-control: private
<DT>02.09.2016 12:43:50</DT>
6184
Date time>>02.09.2016 12:43:50
12:43:50 2016/9/2
AddSecond(78)
12:45:8 2016/9/2
AddMinute(83)
14:8:8 2016/9/2
AddHour(33)
23:8:8 2016/9/3
AddDay(46)
23:8:8 2016/10/18
AddMonth(18)
23:8:8 2018/4/18
AddYear(21)
23:8:8 2039/4/18
Disconnected


B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial

   Private wifi As ESP8266WiFi
   Private astream As AsyncStreams
   Private socket As WiFiSocket

   Public Time_HH As Int
   Public Time_MM As Int
   Public Time_SS As Int
   Public Date_DD As Int
   Public Date_MM As Int
   Public Date_YY As Int

End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
  '**************** ASP file**** datetime.asp *****************
    ' you can read on web page form internet
' asp file code:
'<%=now()%>

ReadDate("01.09.2016 16:49:45".GetBytes)
End Sub



Sub ReadDate(data() As Byte)

    Dim bc As ByteConverter
    Log("Date time>>",data) ' Date time>>01.09.2016 16:49:45
    'Log("Gün:", x(0))
     'step 1: get the correct substring
   'step 2: convert it to string as array of bytes cannot be directly converted to numbers
   Date_DD=bc.StringFromBytes(bc.SubString2(data, 0, 2))
   Date_MM=bc.StringFromBytes(bc.SubString2(data, 3, 5))
   Date_YY=bc.StringFromBytes(bc.SubString2(data, 6, 10))

   Time_HH=bc.StringFromBytes(bc.SubString2(data, 11, 13))
   Time_MM=bc.StringFromBytes(bc.SubString2(data, 14, 16))
   Time_SS=bc.StringFromBytes(bc.SubString2(data, 17, 19))
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Time_SS=AddSecond(78)
   Log("AddSecond(78)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Time_MM=AddMinute(83)
   Log("AddMinute(83)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Time_HH=AddHour(33)
   Log("AddHour(33)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Date_DD=AddDay(46)
   Log("AddDay(46)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Date_MM=AddMonth(18)
   Log("AddMonth(18)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)
   Date_YY=AddYear(21)
   Log("AddYear(21)")
   Log(Time_HH,":",Time_MM,":",Time_SS,"  ",Date_YY,"/",Date_MM,"/",Date_DD)

End Sub

Sub AddDateTime(Year As Int,Month As Int,Day As Int,Hour As Int,Mint As Int, Secd As Int)
    If Secd <>0 Then
        Time_SS=AddSecond(Secd)
    End If
    If Mint<>0 Then
        Time_MM=AddMinute(Mint)
    End If
    If Hour<>0 Then
        Time_HH=AddHour(Hour)
    End If
    If Day<>0 Then
        Date_DD=AddDay(Day)
    End If
    If Month<>0 Then
        Date_MM=AddMonth(Month)
    End If
    If Year<>0 Then
        Date_YY=AddYear(Year)
    End If

End Sub

Sub AddSecond(Sec As Int) As Int
    Dim Time_SSx As Int
    Time_SSx=Time_SS+Sec
    If Time_SSx>59 Then
        Time_MM= AddMinute((Time_SSx-(Time_SSx Mod 60))/60)
        Return (Time_SSx Mod 60)
    Else if Time_SSx<0 Then
        Time_MM= AddMinute((Time_SSx-(Time_SSx Mod 60))/60-1)
        Return (Time_SSx Mod 60)+60
    Else
        Return Time_SSx
    End If
End Sub
Sub AddMinute(minut As Int) As Int
    Dim Time_MMx As Int
    Time_MMx=Time_MM+minut
    If Time_MMx>59 Then
        Time_HH= AddHour((Time_MMx-(Time_MMx Mod 60))/60)
        Return (Time_MMx Mod 60)
    Else if Time_MMx<0 Then
        Time_HH= AddHour((Time_MMx-(Time_MMx Mod 60))/60-1)
        Return (Time_MMx Mod 60)+60
    Else
        Return Time_MMx
    End If
End Sub
Sub AddHour(Hour As Int) As Int
    Dim Time_HHx As Int
    Time_HHx=Time_HH+Hour
    If Time_HHx>23 Then
        Date_DD= AddDay((Time_HHx-(Time_HHx Mod 24))/24)
        Return (Time_HHx Mod 24)
    Else if Time_HHx<0 Then
        Date_DD= AddDay((Time_HHx-(Time_HHx Mod 24))/24-1)
        Return (Time_HHx Mod 24)+24
    Else
        Return Time_HHx
    End If
End Sub

Sub GetDays(Mount As Int,Year As Int) As Int
    Dim limit As Int=0
    Dim mnt As Int=Mount
    Dim yr As Int
    yr=Year
    If Mount<1 Then
        mnt=Mount+12
        yr=yr-1
        If mnt<1 Then
            mnt=mnt+12
            yr=yr-1
            If mnt<1 Then
                mnt=mnt+12
                yr=yr-1
            End If
        End If
    End If
 
 
    Select mnt
        Case 1,3,5,7,8,10,12
            limit=31
        Case 4,6,9,11
            limit=30
        Case 2
            If (yr Mod 4)=0 Then limit=29 Else limit=28
    End Select
 
    Return limit

End Sub
Sub AddDay(day As Int) As Int
    Dim mnt As Int=0
    Dim lmt As Int=0
    mnt=Date_MM-1
    If mnt<1 Then mnt=12
    Dim limit As Int
    limit=GetDays(Date_MM,Date_YY)
    Dim Date_DDx As Int
    Date_DDx=Date_DD+day
    If  Date_DDx>limit Then
     Date_MM=AddMonth((Date_DDx-(Date_DDx Mod limit))/limit)
     Return (Date_DDx Mod limit)
    Else If Date_DDx<0 Then
        Dim i As Int=1
        Dim Date_MMx As Int=0
        Do Until (Date_DDx+GetDays(Date_MM-i,Date_YY))>0
            Date_DDx=Date_DDx+GetDays(Date_MM-i,Date_YY)
            Date_MMx=Date_MMx-1
            i=i+1
        Loop
        If Date_DDx<1 Then
            Date_MM=AddMonth(Date_MMx-1)
            Return (Date_DDx+GetDays(Date_MM,Date_YY))
        Else
            Log("else...PROBLEMMMMMMMM")
            Return 1
        End If 
    Else
     
        Return Date_DDx
    End If
End Sub

Sub AddMonth(Month As Int) As Int
    Dim Date_MMx As Int
    Date_MMx=Date_MM+Month
    If Date_MMx>12 Then
        Date_YY= AddYear((Date_MMx-(Date_MMx Mod 12))/12)
        Return (Date_MMx Mod 12)
    Else if Date_MMx<0 Then
        Date_DD= AddDay((Date_MMx-(Date_MMx Mod 12))/12-1)
        Return (Date_MMx Mod 12)+12
    Else
        Return Date_MMx
    End If
End Sub

Sub AddYear(Year As Int) As Int
    Return (Date_YY+Year)
End Sub
 
Top