"Basic" question about detecting 12 and 24 hour time

devpro

New Member
Licensed User
Longtime User
Howdy All,

Have been playing with .. errrrr I mean, experimenting (posture improves) with b4a since last week. I've been impressed with it since I downloaded it! So much so that I bought it!! Used basic back in the day and this is a welcome second experience. Anyways enough ranting, my question follows.

How can I detect AM, PM and 24hr time? What I'm looking for something like this:

function functAMPM24hr returns 0 for 24hr clock, 1 for AM, or 2 for PM.

A code example would be great.

TIA

dep


=====
"Making the simple complicated is commonplace; making the complicated simple, awesomely simple, that’s creativity.” --Charles Mingus

You too can be a b4a professional! Buy a copy today! Use code: bekfts when purchasing Basic4android Enterprise version and get it for half price (50%)!!!! Basic4android (Basic for Android) - Android programming with Gui designer
 

pluton

Active Member
Licensed User
Longtime User
I didn't work with time in B4A but did you try something like this:

B4X:
Dim x As Int
x = DateTime.GetHour(now)
If x > 11 Then
ToastMessageShow("Now is PM (" &x& ")", False)
Else
ToastMessageShow("Now is AM (" &x& ")", False)
End If
 
Upvote 0

COBRASoft

Active Member
Licensed User
Longtime User
In addition to this, it would be nice to know the system time offset. This is handy for daylight saving time. Any1?
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Hi,
If you use the following code
B4X:
Dim timeformat As String
   timeformat = DateTime.DeviceDefaultTimeFormat
the string "timeformat" will contain the current set time format, for my 24hr setting it is "HH:mm:ss" for a 12hr setting it will be something like "h:mm a"
if you check SimpleDateFormat (Java 2 Platform SE v1.4.2) it explains all of the datetime possibilities.
The code in the previous post will only work if it is after midday.
Edward
 
Upvote 0

pluton

Active Member
Licensed User
Longtime User
The code in the previous post will only work if it is after midday.
Edward

If you change Time format from 24hour to AM/PM the x will again return time in 24 hour format

See screenshot:

24Hour format set under Settings----------------AM/PM set under settings (look at time in the right corner)

image.png
image.png
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Hi,
Having just tried changing my phone between 12hr and 24hr it does not give any different value for timeformat (so code I posted is no use) , I think the problem is the definition of 'DeviceDefaultTimeFormat'
DeviceDefaultTimeFormat As String [read only]
Returns the default time format based on the device selected language
so no matter time format you set your phone to the device format is still based on the language set.
The x in the code will be for 11:00 (24hr) will be 11 in the 12hr as well but the code thinks it is am so code is unreliable .
Edward
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Give this code a shot:

B4X:
Hour = DateTime.GetHour(DateTime.Now)
Minutes = DateTime.GetMinute(DateTime.Now)
Seconds = DateTime.GetSecond(DateTime.Now)

If Hour < 12 Then Meridian = " AM"
If Hour = 12 Then Meridian = " PM"
If Hour = 0 Then

   Hour = 12
   Meridian = " AM"

End If

If Hour > 12 Then

   Hour = Hour - 12
   Meridian = " PM"

End If

TheTime = NumberFormat(Hour, 2, 0) & ":" & NumberFormat(Minutes, 2, 0) & ":" & NumberFormat(Seconds, 2, 0) & Meridian

Msgbox("The time is " & TheTime, "")
 
Last edited:
Upvote 0

devpro

New Member
Licensed User
Longtime User
Thanks guys! :)

My journey into b4a country continues.

=========

“A journey of a thousand miles must begin with a single step.” -– Lao Tzu

What are you waiting for? Be a b4a professional TODAY! Use code: bekfts when purchasing Basic4android Enterprise version and get 50% off!!!!Basic4android (click here)
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
How can I detect AM, PM and 24hr time? What I'm looking for something like this:

function functAMPM24hr returns 0 for 24hr clock, 1 for AM, or 2 for PM.

Sorry, it's not really clear for me what you want to do.

Do you want to find out the system settings if 24h display is enabled? Or do you want to display a ticks value in the correct format?

For getting System settings you can use

Phone.GetSettings("time_12_24") to check if 24h display is enabled on your device.

For getting just AM/PM strings for a ticks value you can use AHDateTime object from AHLocale Library I think.
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
For getting System settings you can use

Phone.GetSettings("time_12_24") to check if 24h display is enabled on your device.
Spot on Markus,
that did the trick. I must sit down some day and go thru the Android developers site there is a lot of info there Settings.System | Android Developers
Ticks value will be the same for am or pm just specify the
B4X:
DateTime.TimeFormat = "HH:mm:ss"
or
B4X:
DateTime.TimeFormat = "h:mm:ss a"
to get your required version of 12hr or 24hr
 
Upvote 0
Top