I guess my question would be more like: what is the difference between dateString1 and dateString2?
NSString *dateString = [NSString stringWithFormat:@"2012-07-09 04:10:00 +0000"];
NSString *dateString2 = [NSString stringWithFormat:[[list objectAtIndex:0] nsDate]];
NSArray *chunks = [dateString2 componentsSeparatedByString:@">"];
NSString *correct = [chunks objectAtIndex:0];
NSLog(@"StingOne:%@", dateString);
NSLog(@"InvalidTwo:%@", dateString2);
NSLog(@"StingTwo:%@", correct);
NSDate *datefromString = [[NSDate alloc] init];
NSDate *datefromString2 = [[NSDate alloc] init];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd hh:mm:ss z"];
NSDateFormatter *formater2 = [[NSDateFormatter alloc] init];
[formater2 setDateFormat:@"yyyy-MM-dd hh:mm:ss z"];
datefromString = [formater dateFromString:dateString];
datefromString2 = [formater2 dateFromString:correct];
NSLog(@"1st DATE:%@", datefromString);
NSLog(@"2nd DATE:%@", datefromString2);
As you can see in this code I am converting two strings into NSDate. This is what I get in my log console:
2012-04-24 17:22:09.586 NSString2NSDate[20233:f803] StingOne:2012-07-09 04:10:00 +0000
2012-04-24 17:22:09.589 NSString2NSDate[20233:f803] InvalidTwo:2012-07-19 23:00:00 +0000>
2012-04-24 17:22:09.590 NSString2NSDate[20233:f803] StingTwo:2012-07-19 23:00:00 +0000
2012-04-24 17:22:09.594 NSString2NSDate[20233:f803] 1st DATE:2012-07-09 04:10:00 +0000
2012-04-24 17:22:09.595 NSString2NSDate[20233:f803] 2nd DATE:(null)
I can’t get my mind on what is the problem here because both strings are outputting correct NSLog as strings. I notice that dateString2 parsed from xml had “>” at the end which is definitely added somewhere in the process so I removed it but still NSDate is (null).
Is this string encoding problem and how do I correct this?
XML file encoding is utf-8.
Thank you.
hhindicates hours in the range1-12andHHindicates hours in the range0-23. So you should useHHinstead ofhhto be able to parse23:00:00.Also note that I’ve used only one
NSDateFormatter. It is not an error to use two, but the second one is not necessary.