0

I need to convert "2013-05-05T00:00:00Z" this string to NSDate and again NSDate to Nsstring. I have tried with different-differnt format style but didn't get any success.

I have already checked.

NSDate from NSString

Convert NSDate to Sql Server Datetime format

Thanks.

// for getting string for Advance search  

-(NSDate*) doGetDateFromStringGSearch :(NSString*) dateStr 
{

NSDateFormatter * outputFormatter4 = [[[NSDateFormatter alloc ] init ] autorelease ];

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[outputFormatter4 setLocale:locale];

//[outputFormatter4 setDateFormat: @"yyyy-MM-dd'T'HH:mm:ss:SSS"];
//[outputFormatter4 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];
 [ outputFormatter4 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss:Z" ];
 //[ outputFormatter4 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss':00Z'" ];

NSString * inputString2 = dateStr;
NSDate * inputRecivedToDate = [outputFormatter4 dateFromString:inputString2 ];
NSLog(@"doGetDateFromString  %@", inputRecivedToDate);

return [[inputRecivedToDate retain] autorelease];
}
Community
  • 1
  • 1
Mangesh
  • 2,257
  • 4
  • 24
  • 51

2 Answers2

3

Change you formatter string to :

[outputFormatter4 setDateFormat:@"yyyy-MM-dd'T'hh:mm:ss'Z'"];

The full code:

NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'hh:mm:ss'Z'"];
NSDate *date=[dateFormatter dateFromString:@"2013-05-05T00:00:00Z"];

NSLog(@"%@",date);
Anupdas
  • 10,211
  • 2
  • 35
  • 60
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Can you check out this [answer](http://stackoverflow.com/a/17010081/767730) and comments. As per ISO8601 character `Z` is a shorthand for UTC. If you ignore it, there would be timezone issues as default timezone of dateFormatter would be device specific. – Anupdas Jun 15 '13 at 06:30
  • This format will work. But it totally ignores the timezone. The presence of the character `Z` denotes that the string is in UTC. If you ignore this Z, you need to set the timezone explicitly to the dateFormatter. If it is not done, there would be erroneous output based on localTimeZone of the device. – Anupdas Jun 15 '13 at 06:40
  • For date "2013-05-05T00:00:00Z" it is giving me "2013-05-04 18:30:00 +0000", it is not proper date :( – Mangesh Jun 15 '13 at 07:28
  • you are in India, which is +5:30 from gmt, so 1830 + 0530 = 0000. – Anoop Vaidya Jun 15 '13 at 07:33
  • Thanks Anoop for quick response. It is not working in case of "2013-06-04T16:55:00Z". If you let me know why it is not working for this date. it would be Great. Thanks – Mangesh Jun 15 '13 at 08:14
  • 1
    @Mangesh This is exactly the issue I was talking about. The dateString is in UTC, with the kind of parsing it is doing now. The date formed is in IST(Indian Standard Time) so there would be a lag of 05:30. You can remove it by setting timeZone UTC to the formatter or use the method that I suggested. – Anupdas Jun 17 '13 at 06:32
  • @Anupdas [df_utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; yes it is working for iOS 6. ill check for iOS 5. Thank you very much. – Mangesh Jun 17 '13 at 06:45
  • @Anupdas: thanks buddy, for adding timezone. this completes my answer, I wish I cud hv given you +1... – Anoop Vaidya Jun 17 '13 at 06:53
  • @Anupdas, Thank you very much, it is working properly but on iOS 5 it return null in case of "2013-03-24T08:18:31.469Z" string while it is working for other string like "2013-05-29T00:11:00Z" – Mangesh Jun 17 '13 at 12:43
  • @Mangesh 2013-03-24T08:18:31.469Z needs a dateFormat `yyyy-dd-MM'T'HH:mm:ss.SSSz` and 2013-05-29T00:11:00Z needs `yyyy-MM-dd'T'HH:mm:ssz`. DateFormatter is very sensitive to the format specified, try to make your dateString uniform in either of cases. – Anupdas Jun 17 '13 at 12:48
  • even you can extract seconds and milliseconds then use formatters – Anoop Vaidya Jun 17 '13 at 12:51
  • Ahhh but it is working in case of iOS 6 :( and it is dynamic. I am not sure which format will come from server. As a quick work around I can check "." in string and then go for specific format. Thanks – Mangesh Jun 17 '13 at 12:53
1

I have been using the NSDateFormatter with @"yyyy-MM-dd'T'HH:mm:ssZZZZ" format for many years, but found out that on few, but only few (maybe less then 1%) of devices it was unsuccessful and was returning nil.

So as a fallback I have been using this library: https://github.com/keithpitt/ISO8601DateFormatter and now I am not seeing any issues with parsing ISO8601 date formats.

The library supports both dateFromString: and stringFromDate:.

Miroslav Kovac
  • 1,168
  • 1
  • 16
  • 27