iOS Question datetimeparse weirdness

kstainsb

Member
Licensed User
Longtime User
My code:

B4X:
DateTime.TimeFormat = "kk:mm:ss"
DateTime.DateFormat = "dd/MM/yyyy"
Log(DateTime.DateTimeParse("28/08/2016","14:30:00"))

If the iOS device time setting is 24-hours, it works. If I set the device to am/pm, the program crashes:

Error occurred on line: 3394 (Main)
Error parsing date string.
Stack Trace: (
CoreFoundation <redacted> + 150
libobjc.A.dylib objc_exception_throw + 38
CoreFoundation <redacted> + 0
0x00180229 -[B4IDateTime NSDateToTicks:] + 124
0x001805a5 -[B4IDateTime DateTimeParse::] + 416
CoreFoundation <redacted> + 68
CoreFoundation <redacted> + 292

Also crashes if I use Timeformat "HH:mm:ss".

Using B4i 2.51, testing on an iPad2, with iOS 9.3.5.

Any ideas?
 
Last edited:

kstainsb

Member
Licensed User
Longtime User
When on 12-hour (am/pm) time display in settings, the iPad works with

B4X:
Log(DateTime.DateTimeParse("28/08/2016","2:30:00 pm"))

So, for anyone else who encounters this bug, you can do this...

B4X:
DateTime.TimeFormat = "kk:mm:ss"
DateTime.DateFormat = "dd/MM/yyyy"
try
   'Time in 24-hour mode..
   Log(DateTime.DateTimeParse("28/08/2016","14:30:00"))
catch
   'Time in 12-hour mode..
   Log(DateTime.DateTimeParse("28/08/2016","2:30:00 pm"))
end try

Of course the second datetimeparse could throw an exception, but I don't think that you can nest try-catches..?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Ah, thanks for trying! The workaround works anyway..

Great product/support btw!

i have the same problem and your solution wont work always.
if the phone language is set to a different language like hebrew and 24h is disabled, then the time wont be "06:00 am" it will be "לח 06:00"
so you better check if the phone is set to 24h or 12h and set the dateformat to the correct format
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User

hi,

thanx for that tip but this is not the perfect solution.
i have users from europe and from usa. some have 12h mode on there phones some 24h mode.
if i will set for all to am/pm the users from europe won't like it.

what i do is, i check if the phone is set to 12h (am/pm) like this:

B4X:
If is24h = False Then AmPmMode = True

Sub is24h As Boolean
    Dim NaObj As NativeObject = Me
    Return NaObj.RunMethod("Clock",Null).AsBoolean 
End Sub

#if OBJC

-(BOOL)Clock{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[NSLocale currentLocale]];
    [formatter setDateStyle:NSDateFormatterNoStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *dateString = [formatter stringFromDate:[NSDate date]];
    NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
    NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
    BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);

    return is24h;
}
#End If

then i use the boolean to set my date/time format:

B4X:
    If AmPmMode Then
        DateTime.DateFormat = "MM/dd/yyyy"
        DateTime.TimeFormat = "h:mm a"
    Else
        DateTime.DateFormat = "dd/MM/yyyy"
        DateTime.TimeFormat = "HH:mm:ss"
    End If

to set the format to the datepicker i do it like this:

B4X:
        If AmPmMode Then
            Dim locale As NativeObject
            locale = locale.Initialize("NSLocale").RuNMethod("alloc", Null).RunMethod("initWithLocaleIdentifier:", Array("en_US")) 'or en_GB
            Dim no As NativeObject = DatePicker1
            no.RunMethod("setLocale:", Array(locale))    
            Log("12h")
        Else
            Dim locale As NativeObject
            locale = locale.Initialize("NSLocale").RuNMethod("alloc", Null).RunMethod("initWithLocaleIdentifier:", Array("en_GB")) 'or en_GB
            Dim no As NativeObject = DatePicker1
            no.RunMethod("setLocale:", Array(locale))    
            Log("24h")            
        End If

in my app it is very important to have the user date and time mode as on his phone so he is used to that mode and wont uninstall the app.
 
Last edited:
Upvote 0
Top