NSDate is not returning my local Time zone /default time zone of device NSDate is not returning my local Time zone /default time zone of device ios ios

NSDate is not returning my local Time zone /default time zone of device


Try this...

NSDate* sourceDate = [NSDate date];NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];

This help respond to the current system timezone.


Forget NSDateFormatter - it's way too complicated, instead try this magic:

NSLog(@"Date with local timezone is: %@",[date descriptionWithLocale:NSLocale.systemLocale]);


NSDate is a "raw" date. That's why it is in GMT. It's up to the code to use NSDateFormatter (as you have done) to output the date to a value that makes sense for the user.

In some rare cases you might need to display an NSDate not using the users time zone (like if you want to display a time in New York time no matter where the user is). Then, you set the time zone on the date formatter to a specific value (again, as you have done).

It's common practice for computers to store all dates in GMT, and then adjust how they are displayed for the user. If you start trying to alter how the date is actually stored you are going to mess up a lot of date handling frameworks that are all assuming your NSDate is in GMT.

As you have seen, when you read in a date via an NSDateFormatter, it's converted from the time entered based on the timezone of the formatter you have set, and then converted to GMT. So what do you think is wrong with what it is doing? Because it's doing just what it should - storing dates in GMT and outputting strings based on a timezone.


matomo