-1

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];
Vashum
  • 853
  • 1
  • 10
  • 24
  • I've played with it around a bit, looks like Its the "IST" that's problematic. There is already a post about it: http://stackoverflow.com/questions/24928666/how-to-format-the-date-to-ist – MCMatan Aug 01 '16 at 12:02
  • Your issue is with the `IST` part, this three letter timezone is not supported correctly. Also you want to set a locale in the date formatter since the string contains language specific date: `dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];` – rckoenes Aug 01 '16 at 12:02
  • As stated by @PiratM you might want to either strip the `IST` from the date string or see if the date string can be changed. – rckoenes Aug 01 '16 at 12:06
  • http://stackoverflow.com/questions/24928666/how-to-format-the-date-to-ist see this – Prashant Tukadiya Aug 01 '16 at 12:18
  • See my answer.It works perfectly. – user3182143 Aug 01 '16 at 13:20
  • Vashum try my answer – user3182143 Aug 01 '16 at 14:03

2 Answers2

2

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"];
rckoenes
  • 69,092
  • 8
  • 134
  • 166
1

I tried your code.First it gives nil.Then I changed LocaleIdentifier to en_IN because

IST stands for both Israel Standard Time, and India Standard Time also it indicates this

NSString *strDate = @"Mon, 01 Aug 2016 04:15 PM IST";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm a zzz"];
dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"];
NSDate *dateStr = [dateFormat dateFromString:strDate];
NSLog(@"The date is - %@",dateStr);

The Printed Result is

The date is - 2016-08-01 06:45:00 +0000

Convert between date formats in Objective-C

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39