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,990
  • 2
  • 22
  • 39

4 Answers4

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
  • 587
  • 6
  • 18
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
  • 690
  • 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
0

Trying to resolve this as well now in 2023.

Attempted to use https://github.com/greghuels/cordova-plugin-24hour-setting without luck under a capacitor project. It gets installed fine with "npm install cordova-plugin-24hour-setting", but unable to reference it within the code.

On the other end, the JS approach does not work.

Thx.

albans
  • 1
  • 1
    This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34068043) – RusArtM Mar 23 '23 at 14:13