3

I have to display the date in my app, where i need to display that only in the format of 12 - hour format, i have used this code to display the date

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:kDateDisplayFormat];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:timeZone];
NSString *todaysDate = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate date]]];
[[self lblPointsDate] setText:todaysDate];

here when i change my iPhone date format to 24-hour it display's in 24-hour format. I need to convert 24-hour to 12-hour format or make it display only in 12-hour format. How can i do this?

AnthonyLambert
  • 8,768
  • 4
  • 37
  • 72
Harish
  • 2,496
  • 4
  • 24
  • 48
  • 3
    Please post you `kDateDisplayFormat`, also I would suggest never to use a static format, but use the `NSDateFormatterStyle` – rckoenes Aug 30 '13 at 12:36
  • see so answer http://stackoverflow.com/questions/11139973/how-to-convert-24-hr-string-time-into-12-hr-time-string-format – Deepesh Aug 30 '13 at 12:36
  • #define kDateDisplayFormat @"dd.M.yyyy / hh a" – Harish Aug 30 '13 at 12:45
  • You are using a correct format `hh` should give you 12-hour format. – rckoenes Aug 30 '13 at 12:49
  • 3
    `[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate date]]]` WHY??? – Kevin Aug 30 '13 at 13:01
  • Actually it will be like : [NSString stringWithFormat:@"Date is :"%@",[dateFormatter stringFromDate:[NSDate date]]] – Harish Aug 30 '13 at 13:34
  • When you change your iPhone 12/24 setting you break things. The 12/24 instruction in the setDateFormat operand are ignored. To get around this you must change the locale, as described [here](http://stackoverflow.com/a/6735644/581994). Of course, if you WANT the iPhone setting to "show through", use a generic format rather than rigidly specifying it. – Hot Licks Aug 30 '13 at 17:03

3 Answers3

5

Just switch

kDateDisplayFormat.dateFormat = @"HH:mm a";

to

kDateDisplayFormat.dateFormat = @"hh:mm a";
Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70
2

Although a solution is to use the 12-hour time format hh and not te 24-hour HH format why not let the user systems preference select the correct format:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;

self.lblPointsDate.text = [dateFormatter stringFromDate:[NSDate date]]

Also there is no need to set the dateFormatter.timeZone to the system timezone, this is set by default.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • The dateFormat you post is correct, can you post a log where print out the date and the result of the dateFormatter? – rckoenes Aug 30 '13 at 13:40
0

Try the following:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"hh:mm a" options:0 locale:[NSLocale currentLocale]]];
NSString *theTime = [dateFormatter stringFromDate:[NSDate date]];
Max
  • 12,622
  • 16
  • 73
  • 101
Shyam Dixit
  • 2,325
  • 3
  • 19
  • 27