How to convert a date-format string into a NSDate - objective-c

I have a NSString that contains this value:
#"19 Oct 2013 18:00:54 GMT"
I want to use that data to convert into a NSDate. I use this code to do so:
NSDateFormatter *dateFormat=[[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MM YYYY HH:mm:ss Z"];
NSDate *eventDate=[dateFormat dateFromString:self.informationPublishDate];
NSLog(#"Date: %#", eventDate);
The problem here is that this code outputs this:
2012-12-23 13:00:51 EST
It outputs this value for events of all dates and times, and I don't know what I'm doing wrong.
Thanks in advance for your help.

The format is wrong. You are missing an M and it should be yyyy instead of YYYY
The correct one is
#"dd MMM yyyy HH:mm:ss Z"

Related

DateFormatter keep returning null

I have a date i take from twitter :
NSString *date=[twt objectForKey:#"created_at"];
NSLog(#"%#",date);
NSDateFormatter *df = [ [NSDateFormatter alloc] init] ;
[df setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSDate *newdate = [df dateFromString:date];
NSLog(#"new: %#",newdate); //null
the date is : Thu Jun 12 20:56:53 +0000 2014 and i get null on the new date .
What am i missing ?
the date formatter for that format (Thu Jun 12 20:56:53 +0000 2014) would be this:
"EEE MMM d HH:mm:ss Z yyyy"
rather than this "dd-MM-yyyy HH:mm:ss".
The date formatting string you've shared sure does't look like it matches the date you're getting from Twitter. The docs for dateFromString state...
A date representation of string interpreted using the receiver’s
current settings. If dateFromString: can not parse the string, returns
nil.
Read up!
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Converting NSString to Epoch timestamp iOS7

I need to convert the following date to epoch timestamp
Wed, 21 May 2014 16:47:00 BST
stringPubDate has the above string stored in it.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMMM yyyy HH:mm:ss z"];
NSDate *date = [dateFormatter dateFromString:stringPubDate];
Null is being returned when I do this. I need to convert this NSString to NSDate and then convert it into epoch timestamp. Can someone please help me?
The problem is that the date format you are giving does not match the format of the date in the string.

NSDate coming null in iOS

I have to get a date from a string. So I am using the below code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//Fri Mar 09 15:31:35 CST 2012
[formatter setDateFormat:#"EEE MMM d HH:mm:ss ZZZ yyyy"];
NSDate *date = [formatter dateFromString:[newDict objectForKey:#"eventDate"]];
NSLog(#"aDate is %#",[newDict objectForKey:#"eventDate"]);
eventDate is coming in the format aDate is Fri May 18 12:00:37 IST 2012 but date is coming null.
I know it is a small issue but I am really stuck up with it.
Your input string can be parsed with a format of #"EEE MMM d HH:mm:ss VVV yyyy".
Try adding this before the dateFromString: call:
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"]];

NSDateFormatter will not parse string with timezone that includes colon

I'm trying to transform #"Fri, 26 Aug 2011 10:51:00 +02:00" into an NSDate:
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
I get nil as a result; what am I doing wrong?
You can use the getObjectValue:forString:range:error: method to parse dates that have the colon in the timezone:
// test date formatting
NSString *dateString = #"2012-04-11T18:34:19+00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
NSDate *date;
NSError *error;
[formatter getObjectValue:&date forString:dateString range:nil error:&error];
The colon in the timezone (+02:00) is the issue. According to the Unicode Standard #35, 1..3 capital Z pattern denotes a RFC 822 time zone. RFC 822 time zones represent the offset from GMT (or UTC) and have the following format:
zone = "UT" / "GMT" ; Universal Time
...
...
/ ( ("+" / "-") 4DIGIT ) ; Local differential
; hours+min. (HHMM)
As you can see, there is no colon between hours and minutes of the time zone. Therefore, the time zone should be +0200.
The most proper solution would be to generate a unicode compliant date string in the first place, but if you are stuck with this format, you may need to preprocess the date string before you pass it to NSDateFormatter.
For example, a quick fix would be using stringByReplacingOccurrencesOfString to get rid of the colon in the time zone:
// dateString --> Fri, 26 Aug 2011 10:51:00 +02:00
dateString = [dateString stringByReplacingOccurrencesOfString:#":"
withString:#""
options:0
range:NSMakeRange(25, [dateString length] - 25)];
// dateString --> Fri, 26 Aug 2011 10:51:00 +0200
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
// date --> 2011-08-26 08:51:00 +0000
No string manipulation required. Change your format string to:
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z:00"];
I tested it and it parses the date you give just fine. When I print the date using [NSDate description], I get 2011-08-26 08:51:00 +0000, which is equivalent to the date given in the string.
Yeah, this is a common problem. A number of servers produce the date with the colon in the timezone, and NSDateFormatter can't (to my knowledge) be convinced to accept it. The solution is to cut out the colon somehow, as suggested.

Objective-C: Unicode Date Format

I am trying to work out how to have the UNICODE representation of
Sun, 03 May 2009 19:58:58 -0700 as eee, dd MMM yyyy HH:mm:s ZZZZ or something. I can't seem to get this working precisely.
Use an NSDateFormatter. It lets you set a particular format string, using the format specifiers from the Unicode spec, then get the formatted date from a given NSDate object using stringFromDate:. Also consider reading Apple's doc about formatting dates. Example:
// Given some NSDate *date
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
NSString *formattedDate = [formatter stringFromDate:date];