简体   繁体   中英

How to check if Cordova app is running in a 12 (AM/PM) or 24 hour environment

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM