I receive a date through a string parameter, which is tempDateString, in a [day month year] format (for ex. 01 05 2005):
NSLog(@"tempdatestring %@", tempDateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MM YYYY"];
NSDate *dayDate = [dateFormatter dateFromString:tempDateString];
NSLog(@"daydate %@", dayDate);
My problem is, that the two logs don't match. The outputs are:
tempdatestring 04 10 2012
daydate 2011-12-24 22:00:00 +0000
What should I change at the date formatter's date format, to get the good date?
Change date format to
@"dd MM yyyy"
. After this, dateFromString may still parse the wrong date (in my case it was yesterday 21-00). To avoid this I've set TimeZone in my DateFormatter:-7200.0 is my timezone, you should change this to yours ("0" sets to Greenwich). After this log looks like:
2 Problems
@"dd MM yyyy"
case sensitiveUse timezone to get the correct value[GMT value]
When you use the
%@
format specifier, the return value of the-description
method invoked on the provided object is used.NSDate
's-description
method outputs its value in that specific way.Your real problem though is that your date format string is incorrect - it should be
dd MM yyyy
.I stuck this in a sample Xcode project:
It gave me the following output:
Do this: