4

Possible Duplicate:
Converting NSString to NSDate (and back again)

I make a request to the flickr api and it returns me the date in the following format

"2013-02-01T06:25:47Z"

How do i convert this into NSDate format??

Community
  • 1
  • 1
vivianaranha
  • 2,781
  • 6
  • 33
  • 47
  • 4
    possible duplicate of [Converting NSString to NSDate (and back again)](http://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and-back-again), http://stackoverflow.com/questions/1353081/nsstring-to-nsdate, http://stackoverflow.com/questions/2618807/iphone-sdk-nsstring-to-nsdate, http://stackoverflow.com/questions/2251777/how-do-i-convert-from-nsstring-to-nsdate – CodaFi Feb 01 '13 at 06:29
  • @vivian: I'll not downvote u(cuz u already get lots of!), but wanna tell u that, as u r gold badge holder(!).. so while posting any question here, just do some research first and then post question if needed...! Thanks – Nayan Feb 01 '13 at 07:19
  • 1
    Dude - I am not here for votes - If I get a solution -My job is accomplished - So thanks for your concern. There are a lot of free people who will answer questions just for points - Why not use this resource... I love stackoverflow :) – vivianaranha Feb 01 '13 at 07:21
  • 4
    for those who are looking how to transform the next time format: __2014-05-12T19:52:56.244Z__ here's the appropriate date formatter format: __[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];__ – SoftDesigner May 12 '14 at 20:16

2 Answers2

22

It's a simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the Dateformatter style with existing style for NSString

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:@"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:@"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:@"HH:mm:ss zzz"];
[dateFormatter setDateFormat:@"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
John Topley
  • 113,588
  • 46
  • 195
  • 237
Madhu
  • 1,542
  • 1
  • 14
  • 30
  • Not an answer. Explain the code above. – CodaFi Feb 01 '13 at 06:34
  • 1
    Its simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the Dateformatter style with existing style for NSString. – Madhu Feb 01 '13 at 06:38
  • 3
    @Madhu, yes, now put that in your answer. Code blocks are worthless learning tools without an explanation/a little documentation. – CodaFi Feb 01 '13 at 06:40
  • @yucelbayram Your welcome :-) Please up-vote for my answer if you think it worths. – Madhu Jun 03 '14 at 13:27
3

You can use below function:

-(NSDate *)getDateFromString:(NSString *)pstrDate
{
    NSDateFormatter *df1 = [[[NSDateFormatter alloc] init] autorelease];
    [df1 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSDate *dtPostDate = [df1 dateFromString:pstrDate];
    return dtPostDate;
}

Hope, it will be helpful to you.

This function will accept your string date and return the NSDate value.

Cheers!

Nishant B
  • 2,897
  • 1
  • 18
  • 25