Question

I know that NSDate doesn't have timezone information.

However, I'm trying to understand how to manipulate them properly.

At the moment I'm passing a date into an object. That date is the user selected date at time 00:00:00.

i.e. if the user hits October 21st then the NSDate passed in should be. 21/10/2013 00:00:00.

However, it isn't it's 20/10/2013 23:00:00. (One hour before).

Now, this is nothing about formatting them or displaying them. I'm just using the NSDates.

I'm creating the date using NSDateComponents and NSCalendar.

I guess my question is...

How can I tell what date an NSDate is actually referring to in my local time zone?

I need to send a UNIX time stamp for 00:00:00 and 23:59:59 for a given date. However, at the moment when I set the hour, minute and second to 0, 0 and 0 then I'm not getting midnight in the current time zone I'm getting midnight in GMT.

This isn't what I want.

Fixed?

OK, I've fixed it... I think. At least, it's doing what I want it to do.

The trick is...

NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateComponents setSecond:timeZone.secondsFromGMT];
Was it helpful?

Solution

I've been confused by this many times. When you NSLog an NSDate, you'll always get the output in GMT. So the 20/10/2013 23:00:00 (GMT) you're seeing is the same as your expected 21/10/2013 00:00:00 (BST). The UNIX timestamp for both of these dates would be the same because it doesn't take into account timezone - it's always UTC.

If you want to output in a user-readable format, an NSDateFormatter will format the date using your current timezone and locale.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top