0

Hi at all I want to parsing a date from XML with this format:

2012/05/25 02:37

- (void) compareDate:(NSString *)dataUno secondaData:(NSString *) dataDue {

    NSDateFormatter *dateFormat= [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];

    NSDate *dt1 = [dateFormat dateFromString:dataUno];
    NSDate *dt2 = [dateFormat dateFromString:dataDue];

    NSLog(@"dt1: %@ dt2: %@", dt2, dt1);
}

but when I try to print the date I get a null value, where is the error?

kikko088

3
  • What calendar has 37 months???
    – Paul.s
    Apr 2, 2012 at 22:40
  • Or 37 days in a 29th month...
    – JustSid
    Apr 2, 2012 at 22:48
  • the number is random number...now I change it but in xml are correct, excuse me, I edit the question now Apr 2, 2012 at 22:50

2 Answers 2

0

Your date formatter's format string "yyyy-MM-dd HH:mm" doesn't match your date format. Actually, "2012/37/29 02:37" isn't a date under any format, but I assume the 37 is a typo and that's supposed to be the month.

Try this:

[dateFormat setDateFormat:@"yyyy/MM/dd HH:mm"];
4
  • the number are random but in xml are correct for example 2012/03/25, excude me for the issue, I edit now the question Apr 2, 2012 at 22:55
  • I try it but is the same...always null Apr 2, 2012 at 22:56
  • Are you sure the strings aren't null? Try logging them right before you use the date formatter.
    – yuji
    Apr 2, 2012 at 22:59
  • solved...I have a wrong character on the string, now work perfectly, thank you! Apr 2, 2012 at 23:01
0

It looks to me like your input string doesn't match the format you specified (it uses "/" instead of "-"), so it's returning NULL.

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.