简体   繁体   English

如何检查Cordova应用程序是否在12(AM / PM)或24小时环境中运行

[英]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 是否可以检测用户的机器使用的是12小时制(am / pm)还是24小时制

I tried below code, But it is not working for chrome default browser 我尝试了以下代码,但是不适用于chrome默认浏览器

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. 请建议任何可提供IOS和Android系统时间格式的cordova插件或JavaScript代码。

use the android and ios cordova app plugin code and information 使用android和ios cordova应用程序插件代码和信息

ios code ios代码

  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 Android代码

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 在Cordova thisAndroidIOS中检查插件开发

I would use normal javascript for this: 我会为此使用普通的javascript:

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

works on every modern browser, it should work in any Cordova app as well. 适用于所有现代浏览器,也应适用于任何Cordova应用。

Then check navigator.language against a map of locales and their time formats. 然后对照地区地图及其时间格式检查navigator.language

I also needed this for a project, so I built cordova-plugin-24hour-setting to support Android and iOS. 我也需要一个项目来使用它,因此我建立了cordova-plugin-24hour-setting来支持Android和iOS。 This is based off of Kalpesh Kikani's helpful answer. 这是基于Kalpesh Kikani的有用答案。 Feel free to use it. 随意使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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