Android Question B4XPreferenceDialog, Time as ticks

james_sgp

Active Member
Licensed User
Longtime User
Hi,
I`m using a PrefDialog to get date and time, date is fine as returned as ticks. However, time is not returned as ticks it gives

[Days=0, Hours=22, IsInitialized=true, Minutes=58, Months=0, Seconds=0, Years=0]

How can I get it as ticks?

Thanks, James
 

emexes

Expert
Licensed User
I haven't used PrefDialog (although it looks good, so maybe I should) but my first guess would be something like:

B4X:
Dim TimeTicks As Long = (PrefTime.Hours * 60 + PrefTime.Minutes) * 60 * 1000

and then with luck you can add that to the PrefDate Ticks, which presumably is set to 00:00:00 midnight at the start of the specified date (local time?), to get a full DateTime as Ticks.

What could possibly go wrnog?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
How can I get it as ticks?
Here it is using the way it is stored in the class. I am pretty sure the time is stored as a period in B4XPreferenceDialog using hours and minutes
B4X:
Dim p As Period
    p.hours =22  
    p.Minutes=58
    Dim tks As Long = p.Hours*DateTime.TicksPerHour+p.Minutes*DateTime.TicksPerMinute
    Log(tks)
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
I`m trying to read the time back from a prefdialog not set a time in a prefdialog.

B4X:
DateTime.Date(Options1.Get("date"))

gets the date field (in ticks) no problem, i then need the time field to so i can combine them.
 
Upvote 0

emexes

Expert
Licensed User
i then need the time field to so i can combine them.

Don't you already have the time field(s) ? per:

time is not returned as ticks it gives

[Days=0, Hours=22, IsInitialized=true, Minutes=58, Months=0, Seconds=0, Years=0]

How can I get it as ticks?

You can convert those Hours=22 and Minutes=58 into a Ticks value using either of the above calculations. @Mahares is slightly better than mine, from a self-documenting clarity perspective. šŸ»
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
I seem to be misunderstanding how to use the solutions as I can't get to work either. Attached is a small project as a example.
 

Attachments

  • Project.zip
    15.1 KB · Views: 66
Upvote 0

emexes

Expert
Licensed User
I seem to be misunderstanding how to use the solutions as I can't get to work either. Attached is a small project as a example.

Try this:

B4X:
Log(Options1.Get("date"))
Log(Otions1.Get("time"))

Dim TempPeriod As Period = Options1.Get("time")
Dim TimeTicks As Long = TempPeriod.Hours * DateTime.TicksPerHour + TempPeriod.Minutes * DateTime.TicksPerMinute

Log("TempPeriod = " & TempPeriod)
Log("TimeTicks = " & TimeTicks)

and with luck you'll get something like this:

Log output:
Logger connected to:  HMD Global Nokia C01 Plus
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Time: 166
1670418000000
[Days=0, Hours=12, IsInitialized=true
, Minutes=34, Months=0, Seconds=0
, Years=0]
TempPeriod = [Days=0, Hours=12, IsInitialized=true
, Minutes=34, Months=0, Seconds=0
, Years=0]
TimeTicks = 45240000
 
Upvote 0
Top