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!
The simplest answer with Swift:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "a"
let currentAMPMFormat = dateFormatter.stringFromDate(NSDate()).uppercaseString
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With