Vashum Vashum - 1 year ago 145
Objective-C Question

Custom date string to NSDate

I am trying to convert

Mon, 01 Aug 2016 04:15 PM IST
to NSDate

I tried using the below code but always stuck with nil. Please help

NSString *fullTime = @"Mon, 01 Aug 2016 04:15 PM IST";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh:mm a ZZZ"];
NSDate *tempTime = [dateFormatter dateFromString:fullTime];

Answer Source

You main issue is the IST in the date, IST is not a standard and can stand for many time zones:

IST Indian Standard Time UTC+05:30

IST Irish Standard Time UTC+01

IST Israel Standard Time UTC+02

The date formatter will not be able to correctly format the date since it does not know which timezone is meant.

If you can you should have the date changed or remove the IST part from the date.

Also you will need to add a locale to the date formatter you it knows in which language is used in the date string. For english beste use en_US_POSIX:

dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download