0

Is it possible to detect if user's machine is using 12 hour clock (am/pm) or 24 hour clock

I tried below code, But it is not working for chrome default browser

var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
var dateString = date.toLocaleTimeString();

if (dateString.match(/am|pm/i) || date.toString().match(/am|pm/i) )
{
    //12 hour clock
}
else
{
    //24 hour clock
}

Please suggest any cordova plugin or javascript code which will provide my system time format for both IOS and android.

Rajendra Khabiya
  • 1,980
  • 2
  • 21
  • 39

3 Answers3

1

use the android and ios cordova app plugin code and information

ios code

  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);

  NSLog(@"IS 24 h format%@\n",(is24h ? @"YES" : @"NO"));

Android Code

String value = android.provider.Settings.System.getString(context.getContentResolver(), android.provider.Settings.System.TIME_12_24);

Check the Plugin Development in Cordova this or Android Or IOS

Kalpesh Kikani
  • 597
  • 5
  • 17
1

I would use normal javascript for this:

var dateStringToLookAt = (new Date).toLocaleString(); 

works on every modern browser, it should work in any Cordova app as well.

Then check navigator.language against a map of locales and their time formats.

Oscar Bout
  • 680
  • 5
  • 19
1

I also needed this for a project, so I built cordova-plugin-24hour-setting to support Android and iOS. This is based off of Kalpesh Kikani's helpful answer. Feel free to use it.

Greg H
  • 445
  • 4
  • 12