iOS Question is device clock set to am/pm?

ilan

Expert
Licensed User
Longtime User
hi

i would like to know (when my app starts) if the device clock is set to AM/PM or 24h

i found this:

B4X:
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);
[formatter release];
NSLog(@"%@\n",(is24h ? @"YES" : @"NO"));

http://stackoverflow.com/questions/7448360/detect-if-time-format-is-in-12hr-or-24hr-format

would this work in b4i?
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

you can use the code by the following:

B4X:
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

However, this should also do the trick:

B4X:
Sub is24h As Boolean
    Return Not(DateTime.DeviceDefaultTimeFormat.Contains("a"))
End Sub

Jan
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
thanx @JanPRO :)

However, this should also do the trick:

B4X:
Sub is24h As Boolean
    Return Not(DateTime.DeviceDefaultTimeFormat.Contains("a"))
End Sub
Jan

this will work only if your device language is set to english so you cannot count on that trick (this is the first thing that came to my mind solving this issue ;))
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
What i still could do is replacing : and space with an empty string and check if what is left is a number. if it will contain any letters it will return false
 
Upvote 0
Top