0

I try to get and manage date with NSDateFormatter but how to make it works. I think it's a problem with my first "dateFormatter" which have not the right format, because when I use the second formatter with function "stringFromDate" and paramter "[NSDate date]" it works well, but not when I use my NSDate "dateFromString".

//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:@"to-date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d, yyyy"];

//NSString *toDate = [dateFormatter stringFromDate:dateFromString];
NSString *toDate = [dateFormatter stringFromDate:[NSDate date]];

Thank you for your help :)

2
  • Sorry for the display, I made a mistake.
    – Guillaume
    May 26, 2011 at 16:41
  • What is in 'dateString' before you send it to the dateFormatter?
    – Joe
    May 26, 2011 at 17:15

2 Answers 2

1

Well of course, the DateFormat is not the same as the one you are receiving from the server.

Try this:

dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:[dateString stringByReplacingOccurrencesOfString:@"Z" withString:@" +0000"]];
0
0

Assuming that to-date is a string and not already an NSDate try this instead:

//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:@"to-date"];
NSDate *dateFromString = [[NSDate dateFromString:dateString];

If you want to display a date to the user formatted in a particular format you can then use set up an NSDateFormatter and use stringFromDate: to get a string to show the user. If it still doesn't display correctly set the calendar (I forget if you always have to set it or if there is a default).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.