Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get AM / PM from users set time on iOS

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!

like image 854
Neil Avatar asked Nov 29 '22 09:11

Neil


2 Answers

The simplest answer with Swift:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "a"
let currentAMPMFormat = dateFormatter.stringFromDate(NSDate()).uppercaseString
like image 23
Bartłomiej Semańczyk Avatar answered Dec 09 '22 08:12

Bartłomiej Semańczyk


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];
like image 173
PRNDL Development Studios Avatar answered Dec 09 '22 10:12

PRNDL Development Studios