Reputation: 2058
I am wondering if there is a way that you can get am/pm from the users set time on iOS.
I am currently using this method to get time:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH"];
hours.text = [formatter stringFromDate:[NSDate date]];
[formatter setDateFormat:@"m"];
minutes.text = [formatter stringFromDate:[NSDate date]];
if ([minutes.text intValue] < 10) {
minutes.text = [NSString stringWithFormat:@"0%@",[formatter stringFromDate:[NSDate date]]];
}
[formatter release];
I know I am using HH to get the hours and for the 12 hour format I have to use "hh" bu this does not solve the problem, I need to retrieve the AM/PM.
Hopefully someone can help!
Upvotes: 4
Views: 10242
Reputation: 61774
The simplest answer with Swift:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "a"
let currentAMPMFormat = dateFormatter.stringFromDate(NSDate()).uppercaseString
Upvotes: 3
Reputation: 3300
Here you go. This is the date with am/pm formatting.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss a"];
NSLog(@"Today's Date and Time: %@", [formatter stringFromDate:[NSDate date]]);
[formatter release];
Upvotes: 21