-1

Please how do I make my stringWithFormat/initWithFormat to display am/pm instead of 24hrs, even though users selects am/pm from the pickerviewer? When a user select time; eg 4:15 pm, instead of it to display 4:15pm as confirmation, it displays 16:15 instead. I want it as 4:15pm. PLEASE HELP!!

-(IBAction)datePickerValueChanged:(id)sender
{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setDateFormat:@"h:mm:ss a"];
NSString *strDate = [dateFormatter stringFromDate:self.pick.date];

dateArray = [strDate componentsSeparatedByString:@":"];

NSDate *current = [NSDate date];
NSString *currentTime = [dateFormatter stringFromDate:current];
NSArray *currArray = [currentTime componentsSeparatedByString:@":"];

curr = [currArray[0] intValue]*60 + [currArray[1] intValue];
min = [currArray[0] intValue]*60 + [currArray[1] intValue] + 30;
next = [[dateArray objectAtIndex:0] intValue]*60 +[[dateArray objectAtIndex:1] intValue];

....This is where values are displayed:

             NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
             [dateFormatter setDateFormat:@"yyyy-MM-dd"];
             NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];

             time = [[NSString alloc]initWithFormat:@"%@T%@:%@:00",strDate, [dateArray objectAtIndex:0], [dateArray objectAtIndex:1]];
             NSString *msg = [[NSString alloc] initWithFormat:@"%@ %@ %@ %@ ?", NSLocalizedString(@"do_ticket", nil), self.category.description,NSLocalizedString(@"at", nil),[[NSString alloc]initWithFormat:@"%@:%@", [dateArray objectAtIndex:0], [dateArray objectAtIndex:1]]];
             alert_tick = [[UIAlertView alloc] initWithTitle:nil
                                                     message:msg
                                                    delegate:self
                                           cancelButtonTitle:nil
                                           otherButtonTitles:NSLocalizedString(@"Yes", nil), NSLocalizedString(@"No", nil), nil];
             [alert_tick show];
}
senimii
  • 247
  • 2
  • 10
  • 2
    Why aren't you using an `NSDateFormatter` to format the date? – trojanfoe Jul 24 '14 at 09:36
  • This is same as http://stackoverflow.com/questions/24929005/issues-with-initwithformat-value-from-server/24929073#24929073 – senimii Jul 24 '14 at 09:36
  • I am not getting values from the server in am/pm, it returns it in 24hrs – senimii Jul 24 '14 at 09:39
  • The above code makes little sense. If you need the date formatted a certain way you can use NSDateFormatter to produce virtually any format you wish -- no need to use initWithFormat as you do above. And if you want the date broken into components you can use NSCalendar and NSDateComponents. – Hot Licks Jul 24 '14 at 15:47
  • And understand that if you NSLog an NSDate it will ALWAYS be displayed in 24hour format and GMT timezone. An NSLog contains no formatting info or timezone info but is an absolute point in time. – Hot Licks Jul 24 '14 at 15:50

2 Answers2

0

Use NSDateFormatter. It doesn't matter that server values are given in 24h format, because your date formatting string can accommodate that (i.e. instead of using "h" for hours when calling setDateFormat:, which represents am/pm format, use "H" for 24h format).

Kamaros
  • 4,536
  • 1
  • 24
  • 39
0

Use the format @"hh:mm a" instead.

Example:

NSDate *today = [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];
NSDate *formattedDate = [formatter stringFromDate:today];
NSLog(@"date : %@", formattedDate);

Output:

date : 09:58 AM
ansible
  • 3,569
  • 2
  • 18
  • 29
  • Note that unless the formatter's locale is explicitly set to one of the "special" values (as the OP does at one point in the code he cribbed above), the above will fail if the user sets his phone's 12/24 value contrary to the phone's locale. – Hot Licks Jul 24 '14 at 15:43