Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to NSDate in iphone? [duplicate]

Tags:

iphone

I am geting below date from the server now I want to convert string to date.

Wed, 08 Jun 2011 11:58 pm EDT

But I cant success please give me advice. Thanks

like image 383
ManthanDalal Avatar asked Jun 09 '11 05:06

ManthanDalal


2 Answers

How-To: Convert a string to NSDate

In Objective-C, you commonly nead to convert a string to an NSDate object. A simple way to do this is using the NSDateFormatter object. It provides the dateFromString method which converts a string into an NSDate object. You do, however, need to tell NSDateFormatter the format the date will be in. See below example:

NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd HH:mm:ss a"]; NSDate *myDate = [df dateFromString: myDateAsAStringValue]; 

And read about EDT – Eastern Daylight Time I think it would be better you change EDT to -0004.

like image 103
Viktor Apoyan Avatar answered Sep 22 '22 13:09

Viktor Apoyan


ViTo basically answers this, but the precise format you're looking at for the string you are concerned with is:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEE, dd MMM y hh:mm a zzz"]; NSLog(@"Date: %@", [dateFormatter dateFromString:@"Wed, 08 Jun 2011 11:58 pm EDT"]); 

Take a look at the date format patterns. I add this because it wasn't immediately obvious where to find a description of the pattern.

like image 40
Ben Flynn Avatar answered Sep 21 '22 13:09

Ben Flynn