NSDateFormatter time not changing according to phones settings

霸气de小男生 提交于 2019-12-08 20:20:31
kb920

Try this code

NSDate *time;
if ([objShare checkTimeFormat]){
    [timeFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];;
    [timeFormat setDateFormat:@"hh:mm a"];
    NSLog(@"%@",lblVtime.text);
    time = [timeFormat dateFromString:lblVtime.text];
}
else{
     NSLog(@"%@",lblVtime.text);
    [timeFormat setDateFormat:@"hh:mm a"];
    time = [timeFormat dateFromString:lblVtime.text];
}
[timeFormat setDateFormat:@"HH:mm"];
NSLog(@"%@",[timeFormat stringFromDate:time]);

Let me know if you have any query

The checkTimeFormat method:

-(BOOL)checkTimeFormat{
     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);
     return is24h;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!