My local and default time zone is GMT +5 but when I get date and time by NSDate it return me GMT date and time.
For example the code and output from my code while testing on device is as, [device time zone Islamabad GMT +5]
NSTimeZone *lo = [NSTimeZone localTimeZone];
NSLog(@" - current local timezone is %@",lo); // GMT +5
2010-12-28 20:56:11.785 Done[484:307] - current local timezone is Local Time Zone (Asia/Karachi (GMT+05:00) offset 18000)
NSTimeZone *df = [NSTimeZone defaultTimeZone];
NSLog(@" - current default timezone is %@",df); // GMT +5
2010-12-28 20:56:11.790 Done[484:307] - current default timezone is Asia/Karachi (GMT+05:00) offset 18000
but
NSDate *cDate = [NSDate date];
NSLog(@"current date by NSDate %@",cDate); //but NSDate show GMT
2010-12-28 20:56:11.794 Done[484:307] current date by NSDate 2010-12-28 15:56:11 GMT
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";
//// NSTimeZone *gmt = [NSTimeZone ]
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT+05:00"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@" date string object %@" ,timeStamp); // string From Date is GMT +5
2010-12-28 20:56:11.802 Done[484:307] date string object 2010-12-28T20:56
NSDate *datef = [dateFormatter dateFromString:timeStamp];
NSLog(@" date object %@" ,datef); // the date form above string gives again GMT
2010-12-28 20:56:11.809 Done[484:307] **date object 2010-12-28 15:56:00 GMT**
Why is NSDate not giving local current time? Please help...
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.
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