iOS Question DateTime Question

mcGeorge

Member
Licensed User
Longtime User
Hi @all,

I need a datetime string in this format: "7:20 PM 7/26/2016"
I use the following code:

B4X:
DateTime.TimeFormat = "h:mm a"
DateTime.DateFormat = "M/d/yyyy"
Label1.Text = DateTime.Time( DateTime.Now ) & " " & DateTime.Date( DateTime.Now )

But in Label1.Text is "19:20 7/26/2016".

Why? What is my mistake.

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested it with this code:
B4X:
Private Sub Application_Start ( Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Dim t As Long = DateTime.DateTimeParse("08/15/2016", "19:12:34")
   DateTime.TimeFormat = "h:mm a"
   DateTime.DateFormat = "M/d/yyyy"
   Log(DateTime.Time( t) & " " & DateTime.Date( t))
End Sub

The output is: 7:12 PM 8/15/2016
 
Upvote 0

ilan

Expert
Licensed User
Longtime User

when i try your code i get the result you describe.

 
Upvote 0

mcGeorge

Member
Licensed User
Longtime User
Hi, that is fine, but I have a different result. Lock at the picture and you see it.



I think the reason is my german notebook!?

Do you have an idea?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Hi, that is fine, but I have a different result. Lock at the picture and you see it.

View attachment 47022

I think the reason is my german notebook!?

Do you have an idea?

change your "Get_Date" sub to this:

B4X:
Sub Get_Date As String
    DateTime.DateFormat = "M/d/yyyy"
    DateTime.TimeFormat = "h:mm a"
    Return DateTime.Time(DateTime.Now) & " " & DateTime.Date(DateTime.Now)
End Sub

you need to set the format before you convert long -> String!
 
Upvote 0

mcGeorge

Member
Licensed User
Longtime User
Hi,

@ilan I do exact this in "pb_Start_Click" and you see the result -> 19:52 and not 7:52 PM

I tray the same on my other Notebook with Windows 10 english and it is ok. This is a mistake on German Windows version.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Hi,

@ilan I do exact this in "pb_Start_Click" and you see the result -> 19:52 and not 7:52 PM

I tray the same on my other Notebook with Windows 10 english and it is ok. This is a mistake on German Windows version.

sorry i havenot seen it.

it is really weird, i cannot understand why u r getting that result. the line

B4X:
DateTime.TimeFormat = "h:mm a"

is completely ignored. when u put an "a" in the format u should get either AM/PM even if you set to 24h format u still should get an AM/PM and you dont get it what means this line is ignored.

try to delete your ipa from your device and reinstall the debug bridge and debug your app again. are u using a local mac?


try to create a different project with only that code and see if u get the right result.
u should get "AM/PM" when u enter "a" in the timeformat so even if u use 24h like


B4X:
DateTime.TimeFormat = "HH:mm a"

u still should get "AM/PM"
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…