0

The issue I am having while converting date string to NSDate. For all other locales, below code is working fine and I can generate the NSDate but when phone settings are set region as Ireland and Time format as 12 Hours at that time it gives nil.

NSString *dateString = @"2018-01-04 05:37:01.74";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:locale];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss.SSS"];
NSDate *date = [dateFormat dateFromString:dateString];
Piyush Hirpara
  • 1,279
  • 1
  • 11
  • 29
  • This is working for me. Can you possibly provide the value of `locale` that will cause the error? – Charles Srstka Jan 04 '18 at 16:19
  • Actually, looking at your question again, you're not asking for a 12-hour date here. `HH` in the date format means 24-hour time. If you want 12-hour time, you should use `hh` instead. – Charles Srstka Jan 04 '18 at 16:33
  • @Charles Srstka The HH is set to parse the string date. because in string it is coming as 24 hour format. AFAIK format has to match with the date string I am passing. The Locale I am getting from the device is "en_IE" – Piyush Hirpara Jan 05 '18 at 09:30
  • @vadian this is not duplicate of the question you have mentioned. It is totally different case. In my case even I don't set the locale it does not make any difference because by default it will take the locale set in phone. That is what I am doing manually. – Piyush Hirpara Jan 05 '18 at 09:37
  • @Charles Srstka Try with device set region as Ireland and time format as 12 hours. In simulator it is working for me as well. – Piyush Hirpara Jan 05 '18 at 09:38
  • The linked question suggests to set the locale to "en_US_POSIX" and the year date format to `yyyy`. – vadian Jan 05 '18 at 11:06

0 Answers0