How detect time settings of device?

六月ゝ 毕业季﹏ 提交于 2019-12-12 22:00:22

问题


I have an application in which i have made a alarm clock. In my application problem is that when device date and time settings changed then text of label of alarm also changed. For example if time settings is set as 24 hours then in my label 's text don't show am and pm in last while when change settings as 12 hours then in label's text show am and pm. How detect that what is settings of date and time and how apply validation on label's text that whatever settings always display in am and pm mode or show in 12 hours settings?

Thanks in advances...


回答1:


Use can use this code:
NSDate *today = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings

    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *currentTime = [dateFormatter stringFromDate:today];
    NSLog(@"%@",currentTime);
    [dateFormatter release];
Hear currentTime contains always 24 hour time format.

int hour =  [[[currentTime componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
  if(hour > 12){
//show PM
}else{
//Show AM
}



回答2:


just place this code

[dateFormatter setDateFormat:@"hh:mm a"];

instead of this

[dateFormatter setDateFormat:@"HH:mm"];

then

NSString *currentTime = [dateFormatter stringFromDate:today];
NSLog(@"%@",currentTime);

NSLog will print the current time 12hour format with AM or PM respectively



来源:https://stackoverflow.com/questions/8033099/how-detect-time-settings-of-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!