Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set date format to 24 hour in Objective-c?

I am having problem displaying proper date in my app. When user sets 24 hour format 'ON' in his date time preferences..my app shows correct time but when 24 hour format is set to 'OFF' it shows nothing.

Here is the code I am using:

NSDateFormatter *formatter       =   [[NSDateFormatter alloc] init];

[formatter setDateStyle:style];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
 NSDate *temp            =   [formatter dateFromString:strDate];

[formatter setDateFormat:format];
[formatter setLocale:[NSLocale currentLocale]];

NSString *returnStr     =   [formatter stringFromDate: temp];
[formatter release];
NSLog(@"Return: %@", returnStr);

return returnStr;
like image 877
Manish Verma Avatar asked Mar 19 '13 06:03

Manish Verma


2 Answers

Very simple try this:

For 12 Hrs : (hh:mm:ss)

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

For 24 Hrs : (HH:mm:ss)

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
like image 100
Rajesh Loganathan Avatar answered Sep 20 '22 15:09

Rajesh Loganathan


use this:

Objective-C :

dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

Swift 4.1

dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

after that your date won't modified.

like image 20
KDeogharkar Avatar answered Sep 20 '22 15:09

KDeogharkar