Android Question GetHour, GetMinute, GetSecond

Tjitte Dijkstra

Member
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:

B4X:
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
 

NJDude

Expert
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

Member
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?
 

NJDude

Expert
Licensed User
This code works, compare it to yours and see what you did wrong.
B4X:
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, "")
 

buras3

Active Member
Licensed User
I hope this will help you
B4X:
Sub ConvertToDate(sec As Long) As String
   Dim TempStr As String
   TempStr=NumberFormat(DateTime.GetDayOfMonth(sec),2,0)
   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)
   minutes=minutes-hours*60
  Return NumberFormat(hours,2,0) & ":" & NumberFormat(minutes , 2,0) & ":" & NumberFormat(seconds, 2, 0)
End Sub
 

Tjitte Dijkstra

Member
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?
TD
 
Top