0

I am trying to convert a string into Date in iOS (Swift/Objective C) but I have an issue when the date contains the hours in 24 formats like 2018-11-24_18-11-10

The issue is that when the phone uses 24H option (Setting -> General -> Date & Time) the code works properly but if I disabled this option in the phone the code returns nil

Here is the code in Objective C

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
return [dateFormatter dateFromString:dateString];

and here is it in Swift

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd_HH-mm-ss"
formatter.date(from: dateString)

I tried different formats but in the end, I always get nil if the phone does not enable 24H option in the settings

Willeke
  • 14,578
  • 4
  • 19
  • 47
Mahmoud Adam
  • 5,772
  • 5
  • 41
  • 62

0 Answers0