Android Question Get time and date since epoch with time dialog

jimseng

Active Member
Licensed User
Longtime User
Hello. I want to send a time/date over bluetooth BLE and thought the easiest way was to send it in seconds since epoch. (What I get from DateTime.Now) I copied something I have used in the past as a time picker and stripped out all the datetime.GetHour stuff hoping I would get what I need.

B4X:
    Dim td As TimeDialog
    td.SetTime(DateTime.GetHour(DateTime.Now),DateTime.GetMinute(DateTime.Now),True)
    td.Show("Enter time to start "," Timer","OK","Cancel","",Null)
    Dim newtime As Long = td.TimeTicks
    Log(newtime)
but I get a negative number: -62167399845000
Am I able to achieve this easily using this method?
Thanks
 

JohnC

Expert
Licensed User
Longtime User
ChatGPT says...

The issue you're facing with the negative number (-62167399845000) likely arises from how the date and time are being handled in your B4A (Basic4Android) application, particularly in relation to the TimeTicks property.

Here’s a breakdown of what might be happening and how you can address it:

  1. Understanding TimeTicks:
    • In B4A, TimeTicks returns the number of ticks (milliseconds) since January 1, 1970, but based on how you've set up the TimeDialog, it seems to be incorrectly calculating or retrieving the value. This could be because of improper initialization or setting of the time in the dialog.
  2. Use of DateTime Class:
    • The DateTime.Now in B4A gives you the current date and time, but when you're using DateTime.GetHour(DateTime.Now) and DateTime.GetMinute(DateTime.Now), you're essentially setting the TimeDialog to the current hour and minute but possibly defaulting to a base date (not today’s date), which could be causing the offset leading to negative ticks.
  3. Sending Date/Time Over BLE as Epoch Seconds:
    • Instead of using TimeDialog to set and retrieve the time, which seems to be causing issues, you can directly use the DateTime object to get the current time in seconds since the epoch, which is more straightforward and less error-prone for BLE transmission.
Here’s a simpler and more reliable way to achieve sending the current time/date over Bluetooth BLE in seconds since epoch:
B4X:
Dim currentTime As Long
currentTime = DateTime.Now / 1000 ' Converts milliseconds to seconds
Log(currentTime)
This code snippet will give you the current Unix timestamp in seconds, which you can then send over Bluetooth. If you need to set a specific time rather than the current time, you can use the DateTime methods to set a specific date and time, and then convert that to seconds since the epoch:
B4X:
Sub Process_Globals
   Dim DateTime As Long
End Sub

Sub AppStart
   Dim date As String = "04/25/2024"
   Dim time As String = "15:00:00" ' 3:00 PM
 
   ' Parse the date and time into a DateTime long value
   DateTime = DateTime.DateTimeParse(date, time)
 
   Log("The DateTime in milliseconds since epoch is: " & DateTime)
End Sub
This approach avoids the use of TimeDialog and directly deals with the epoch time, which should be straightforward and error-free for your Bluetooth transmission purposes.
 
Last edited:
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
I get:
Unknown member: settime
Unknown member: setdate
Am I missing something? I also need a method for interactively picking the time and date, although I could manually translate it from the time dialog and set it as you have suggested, if I can get it to work.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I get:
Unknown member: settime
Unknown member: setdate
Am I missing something? I also need a method for interactively picking the time and date, although I could manually translate it from the time dialog and set it as you have suggested, if I can get it to work.
Sorry about that, ChatGPT does hallucinate sometimes with invalid methods.

I updated the code in the previous post with correct code.
 
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
Thanks. I get Unparseable date: "2024-04-25" but I can work with it. i just need to learn about DateTime.DateFormat and DateTime.TimeFormat and I think I have something I can use. Very helpful.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Thanks. I get Unparseable date: "2024-04-25" but I can work with it. i just need to learn about DateTime.DateFormat and DateTime.TimeFormat and I think I have something I can use. Very helpful.
Try using "04/25/2024" - the format probably depends on the devices date format setting.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Hello. I want to send a time/date over bluetooth BLE and thought the easiest way was to send it in seconds since epoch. (What I get from DateTime.Now) I copied something I have used in the past as a time picker and stripped out all the datetime.GetHour stuff hoping I would get what I need.

B4X:
    Dim td As TimeDialog
    td.SetTime(DateTime.GetHour(DateTime.Now),DateTime.GetMinute(DateTime.Now),True)
    td.Show("Enter time to start "," Timer","OK","Cancel","",Null)
    Dim newtime As Long = td.TimeTicks
    Log(newtime)
but I get a negative number: -62167399845000
Am I able to achieve this easily using this method?
Thanks

The return TimeTicks is correct.
When you provide the value of Hour and Minute only, how do you expect the object get what the actual date and time you are referring to?

e.g
B4X:
td.SetTime(11, 40)

The TimeTicks when eveluate returns a default date value in my case is 12/31/0002.
You also need a DateDialog to get the value of user selected Date.

B4X:
Dim HH = DateTime.GetHour(DateTime.Now) As Int 
Dim MM = DateTime.GetMinute(DateTime.Now) As Int
Log("HH = " & HH)
Log("MM = " & MM)
    
Dim td As TimeDialog
td.SetTime(HH, MM, True)
td.Show("Enter time to start "," Timer","OK","Cancel","",Null)
Dim newtime As Long = td.TimeTicks
Log(newtime)
Log(DateTime.Now)
    
Log(DateTime.Date(newtime))
Log(DateTime.Time(newtime))
Log(DateTime.DateFormat)
Log(DateTime.TimeFormat)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
HH = 11
MM = 59
-62167459666000
1714103960836
12/31/0002
11:59:00
MM/dd/yyyy
HH:mm:ss
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
 
Upvote 0
Top