Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the time with AM/PM format in iPhone?

I want to print the time from date picker but with the AM/PM. I can print the time, but it never prints the AM/PM. How can I do this?

like image 899
RJ168 Avatar asked Nov 19 '11 10:11

RJ168


2 Answers

Use below lines of code

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"hh:mm a"];   
    NSString *str_date = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"str_date:%@",str_date);
like image 135
Narayana Avatar answered Oct 18 '22 06:10

Narayana


You could use this document for more information NSDateFormatter Class Reference .An example could be:

NSDate* date = [NSDate date];
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];

[formatter setTimeStyle:NSDateFormatterFullStyle];
NSLog(@"date=%@",[dateFormatter stringFromDate:date]);
like image 43
Hari Avatar answered Oct 18 '22 06:10

Hari