-1

I have a simple question here, though a lot of answers are available but none of them are meeting my expectations.

I want to retrieve my iPhone device date in 24 time hour format. My iPhone time is set as 12 hour time format.

here is code I have tried so far:

    [formatter setLocale:[NSLocale autoupdatingCurrentLocale] ] ;
NSLog(@"date string :%@", [formatter stringFromDate:[NSDate date]]) ;
NSLog(@"Date string :%@\n\n", [formatter dateFromString:[formatter stringFromDate:[NSDate date]]]) ;

[formatter setLocale:[NSLocale currentLocale] ] ;
NSLog(@"Date string :%@", [formatter stringFromDate:[NSDate date]]) ;
NSLog(@"Date string :%@\n\n", [formatter dateFromString:[formatter stringFromDate:[NSDate date]]]) ;

[formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"] ] ;
NSLog(@"New date :%@", [formatter stringFromDate:[NSDate date]]) ;
NSLog(@"Date string :%@\n\n", [formatter dateFromString:[formatter stringFromDate:[NSDate date]]]) ;

[formatter setLocale:[NSLocale systemLocale] ] ;
NSLog(@"Date string :%@", [formatter stringFromDate:[NSDate date]]) ;
NSLog(@"New date :%@\n\n", [formatter dateFromString:[formatter stringFromDate:[NSDate date]]]) ;

    formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:@"yyyy-MM-dd H:mm "] ;
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale currentLocale] localeIdentifier]]];

Outputs:

2015-04-03 12:08:07.494 DateFormatter[4959:2280447] date string :2015-04-03 12:08 PM 
2015-04-03 12:08:07.503 DateFormatter[4959:2280447] Date string :2015-04-03 7:08:00 PM +0000

2015-04-03 12:08:07.504 DateFormatter[4959:2280447] Date string :2015-04-03 12:08 PM 
2015-04-03 12:08:07.505 DateFormatter[4959:2280447] Date string :2015-04-03 7:08:00 PM +0000

2015-04-03 12:08:07.510 DateFormatter[4959:2280447] New date :2015-04-03 12:08 
2015-04-03 12:08:07.511 DateFormatter[4959:2280447] Date string :2015-04-03 7:08:00 PM +0000

2015-04-03 12:08:07.515 DateFormatter[4959:2280447] Date string :2015-04-03 12:08 
2015-04-03 12:08:07.516 DateFormatter[4959:2280447] New date :2015-04-03 7:08:00 PM +0000

The output contains AM or PM label when I am printing date after converting from string.

This code works absolutley when my device time format is 24 hours.

I have tried with "H" and "h" in dateformatter like this.

[formatter setDateFormat:@"yyyy-MM-dd H:mm "] ;

or

[formatter setDateFormat:@"yyyy-MM-dd h:mm "] ;

and many more conbinations :-(

My requirement is to get the date format as 2015-04-03 19:08:00 +0000 for 2015-04-03 7:08:00 PM +0000.

When my device time format is 24 hour, I am getting the expected logs.

Lemme know for any more clarification.

  • possible duplicate of [What is the best way to deal with the NSDateFormatter locale "feature"?](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature) – Hot Licks Apr 03 '15 at 19:37
  • And, of course, consult the format standard at http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns . – Hot Licks Apr 03 '15 at 19:38
  • You claim that the formatted strings all include AM/PM, but that is not the case. Look again at your own data. – Hot Licks Apr 03 '15 at 19:40

2 Answers2

0

I can't comment because I'm a newbie, but try "HH:mm" (two HH's). It's always worked for me.

joeybladb
  • 227
  • 1
  • 10
0

You can try this !!

NSDate* datetime = [NSDate date];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss +0000"];
NSString* dateTimeString = [dateFormatter stringFromDate:datetime];
vivekDas
  • 1,248
  • 8
  • 12