Objective-C: Unicode Date Format - objective-c

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];

Related

How to convert a date-format string into a NSDate

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"

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"]];

Parsing BST Timezone

I'm having problems parsing a date which has 'BST' as its timezone.
This is the date: 2012-04-22 16:00:00 BST
And this is my code
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate * matchDate = [formatter dateFromString:dateStr];
This works well with GMT for example, but for BST I'm getting nil, any clue?
BST is a date format considered a metazone, therefore you should parse it using the V syntax.
I would suggest changing your parse string to:
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss V"];
the use of the quotes around the ZZZ, means that the parser should expect the text ZZZ, rather than a timezone

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.

convert date from one format to another format

HI all,
i have a date string like this
2011-03-31 13:32:02
i want to convert this date string to display like this
Thursday – March 31, 2011
Please help to solve this
Thanks in advance
Use NSDateFormatter. First initialize the formatter with the appropriate format to parse the source string, something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Then use it to parse the date string:
NSDate *date = [formatter dateFromString:string];
Now you can create a new formatter to output in the new format, or just reuse the existing one:
[formatter setDateFormat:#"EEEE - MMMM dd, yyyy"];
NSString *output = [formatter stringFromDate:date];