I have this piece of code:
NSDateFormatter* df = [[NSDateFormatter alloc] init];
df.locale = [NSLocale currentLocale];
df.timeZone = [NSTimeZone localTimeZone];
df.timeStyle = NSDateFormatterShortStyle;
df.dateStyle = NSDateFormatterShortStyle;
df.locale = [NSLocale currentLocale];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *todaysDate = @"31/12/2013";
NSDate* today = [df dateFromString:todaysDate];
NSLog(@"comparing todaysDate: %@ with today: %@",todaysDate,today);
When I check my console I see that todaysDate is 31/12/2013 and today is 2013-12-30 21:00:00 +0000 I do not understand why it goes 1 day into the past... can anyone help me out here? I'm trying to basically filter out some items in a dictionary based on date. Filtering part not included here as it was determined that the dateFromString is not doing its job.
It's very probably because of your TimeZone. You seems to be GMT+3 or something like that. If you change your local Time Zone things should be ok.
You should use : df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]:
instead of localTimeZone
to always have the same results even in different time zones.
Add the below to your NSDateFormatter.
[df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With