Pregunta

I am using Cordova 2.9 in a project.

I use Moment.js to handle a lot of the date/time formatting as the application displays text in English and Mandarin.

I have a feature request that has asked that if the device time formatting is either 12 hour or 24 hour format then the application should reflect this.

Currently we use 24 hour format throughout.

Is there a way to get the time format set in the device and then use that to set the time format at the application level?

¿Fue útil?

Solución

you need to write one plugin for detect time forma.in IOS by objective c we can detect device time format, same as in java (android) also. so check below code and write one plugin.

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

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

for plugin development check this Or Android Or IOS

Otros consejos

You can just cordova's built in globalization plugin. If you run for example,

navigator.globalization.dateToString(
    new Date(),
    function (date) { alert('date: ' + date.value + '\n'); },
    function () { alert('Error getting dateString\n'); },
    { formatLength: 'short', selector: 'date and time' }
);

date.value will hold a dateString that will reveal the time format (just check if "AM" or "PM" is in the string).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top