3

In my app I have to show Date & Time Like this,

10/19/2011 3pm

Currently I write this code,

NSDateFormatter *formatar=[[NSDateFormatter alloc] init];       
[formatar setDateFormat:@"MM/dd/YYYY   HH:mm a"];
[formatar setTimeStyle:NSDateFormatterShortStyle];
NSString *st=[formatar stringFromDate:[NSDate date]];

But Getting date in this format:

10/19/2011 15.00

So, How can i get that in this 10/19/2011 3pm Format?

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Ankit Chauhan
  • 2,375
  • 6
  • 25
  • 37

1 Answers1

11

You need to do some minor changes to the date format to make it work as you need.

1.Your date format should contain only the hour(single-digit) and no minutes.

"MM/dd/YYYY H a" // Still "a" wouldn't not work here

2.The hour should be in 12 hour format ie., "h" not "H" in order for "a" to work. Finally, remove the space between "h" and "a".

"MM/dd/YYYY ha" // Now you will get what you want!
EmptyStack
  • 51,274
  • 23
  • 147
  • 178