1

I have two dates (written in two different NSString), for example

31/12/2011

and

31/03/2012

I have to check if today is in this range.

For read the date from the NSString, I'm doing this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *mydate = [dateFormatter dateFromString:my_string];
NSLog(@"%@", mydate);

but, in this example, the log is this one

2011-12-30 23:00:00 +0000
2012-03-30 23:00:00 +0000

So it's one hour behind. I know that using

NSString *string = [dateFormatter stringFromDate:my_date];

my string it will be ok, but I can't see if today is in the range using NSString (or not?)!

So I have to use NSDate, but how can I solve the "time zone" problem?

Also, I have this problem also using

[NSDate date];

It returns me one hour ago (and I don't want it, because if someone check at midnight, for the phone it will be the day before!)

JAA
  • 1,024
  • 3
  • 20
  • 34
  • possible duplicate of [Still having NSDateFormatter result issues even with NSTimezone properly set, why?](http://stackoverflow.com/questions/6957423/still-having-nsdateformatter-result-issues-even-with-nstimezone-properly-set-wh) – jscs Nov 24 '11 at 17:08

2 Answers2

1

The following should help:

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];

for more details, please check out This question ... It looks similar. The "EST" can be replaced with whichever time zone is required.

Community
  • 1
  • 1
Salil
  • 1,739
  • 2
  • 15
  • 25
  • thanks, now it works... but not for `[NSDate date]` (which is the date right now): with this I have the time one hour ago (because I think it's not using the dateFormatter). How can I do it? – JAA Nov 25 '11 at 10:21
  • You will need dateFormatter to get it to the required timezone. Of course, you can use [NSTimeZone defaultTimeZone] and then calculate the offset by using "-(NSTimeInterval)secondsFromGMT" – Salil Nov 25 '11 at 13:00
0

I may be mistaken, but it looks, like you don't need to take care about time zones in this case. If you have just dates in format dd/MM/yyyy, when you will parse it with NSDateFormatter, it will use current timezone by default. So, when you will be comparing with your today's date all 3 dates will be in the same timezone and date/time adjusting will not be performed.

Denis
  • 6,313
  • 1
  • 28
  • 27
  • 1
    If you do not set the timezone it will offset from GMT. If you set it to 12am 1/1/2012 and do not specify a timezone, it will be 6pm 12/31/2011 in the EST timezone. – Wayne Hartman Nov 24 '11 at 15:59
  • yep, i was not sure about this thing... however, the idea is to have the same timezone for all 3 NSDate values – Denis Nov 24 '11 at 16:00