Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSString to NSDate [duplicate]

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??

like image 995
vivianaranha Avatar asked Nov 29 '22 02:11

vivianaranha


2 Answers

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];
like image 152
Madhu Avatar answered Dec 03 '22 10:12

Madhu


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!

like image 21
Nishant B Avatar answered Dec 03 '22 11:12

Nishant B