PDA

View Full Version : Help with Dateformatter and NSDate




pashik
Nov 27, 2008, 02:11 PM
Hello.
i have date as string "Sat, 29 Nov 2008 00:00:00 -0500"
and i'm trying to create NSDate from it with this code

[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"%a, %d %b %Y %H:%M:%S %Z"];
NSDate *date = [dateFormatter dateFromString:@"Sat, 29 Nov 2008 00:00:00 -0500"];


Date is always nil.

Can someone help me with this?



dejo
Nov 27, 2008, 08:50 PM
You're not using the correct patterns for the setFormatter. Check this out:
http://unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns

More about all of this in the Data Formatting Programming Guide for Cocoa in your iPhone OS Library Documentation.

pashik
Nov 28, 2008, 05:39 AM
You're not using the correct patterns for the setFormatter. Check this out:
http://unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns

More about all of this in the Data Formatting Programming Guide for Cocoa in your iPhone OS Library Documentation.

I used also this format with same result - date is nil


@"EEE, dd MMM yyyy HH:mm:ss ZZ"

dejo
Nov 28, 2008, 11:01 AM
You're starting by defining dateFormatter like so:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
correct?

I tried the following code block and date was not nil:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];
NSDate *date = [dateFormatter dateFromString:@"Sat, 29 Nov 2008 00:00:00 -0500"];

pashik
Dec 6, 2008, 05:52 AM
You're starting by defining dateFormatter like so:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
correct?

I tried the following code block and date was not nil:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];
NSDate *date = [dateFormatter dateFromString:@"Sat, 29 Nov 2008 00:00:00 -0500"];

It looks like after installing sdk 2.2 date is not nil any more even if i set target sdk to 2.0.

thank you.