Android Question GetHour, GetMinute, GetSecond

Similar threads

B4A Tutorial [B4X] [XUI] Creating custom views with XUI
B4A Question Wrong Long Data Type
B4A Question DateTime.GetHour Bug
B4A Tutorial [B4X] Smart String Literal
B4A Code Snippet time switch class

Tjitte Dijkstra

Licensed User
Hi all,

In my program I need to pinpoint the moment the user started.
I tried it this way, but it doesnt work:

Sub START_Click
Dim Begin As String
Dim H,M,S,StartTime As Int
    Begin = DateTime.Time(DateTime.Now) 'this is OK
        DateTime.GetHour(H) 'this returns 0
        DateTime.GetMinute(M) 'this returns 0
        DateTime.GetSecond(S) 'this returns 0
    StartTime = H*3600+M*60+S
display1.Text = Begin & " = 00.00"    'on display1 we see the real time of the moment
display2.Text = StartTime 'but on display2 we see only a zero
End Sub


Licensed User
You are saving the time (ticks) in "Begin", that's what you should use to extract the hours, minutes and seconds. By the way, BEGIN should be a LONG variable.
Last edited:

Tjitte Dijkstra

Licensed User
Thnx so far.
Begin (I DIMed it as a string) cannot be a double. That leads to the error "Invalid double".
But why does DateTime.Gethour(H) always resuklt in a 0?


Licensed User
This code works, compare it to yours and see what you did wrong.
Dim Begin As Long
Dim H, M, S As Int

Begin = DateTime.Now

H = DateTime.GetHour(Begin)
M = DateTime.GetMinute(Begin)
S = DateTime.GetSecond(Begin)

Msgbox(H & ":" & M & ":" & S, "")


Active Member
Licensed User
I hope this will help you
Sub ConvertToDate(sec As Long) As String
   Dim TempStr As String
   TempStr=TempStr & "/" & NumberFormat(DateTime.GetMonth(sec),2,0)
   TempStr=TempStr & "/" & DateTime.GetYear(sec)
   TempStr=TempStr & " " & NumberFormat(DateTime.GetHour(sec),2,0)
   TempStr=TempStr & ":" & NumberFormat(DateTime.GetMinute(sec),2,0)
   Return TempStr
End Sub
Sub ConvertToTimeFormat(sec As Long) As String
  Dim seconds, minutes ,hours As Long
   seconds = sec
  minutes = Floor(seconds / 60)
  seconds = seconds Mod 60
   hours=Floor(minutes / 60)
  Return NumberFormat(hours,2,0) & ":" & NumberFormat(minutes , 2,0) & ":" & NumberFormat(seconds, 2, 0)
End Sub

Tjitte Dijkstra

Licensed User
Thnx all,
After 25 years of experience with Turbo Pascal and Delphi, in which the GetTime works as you should expect, it surprises me that we need so much code in b4A for something simple.
Yet b4A is a great tool.
Maybe the book of Seagrave should have more examples?