2

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...

ios
  • 552
  • 5
  • 22

2 Answers2

0
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
}
Sudesh Kumar
  • 569
  • 3
  • 13
-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

Bala
  • 2,895
  • 1
  • 19
  • 60