0

When i create an object for NSDate and put cursor on that object it was showing currect date like belowenter image description here

and when am trying to log that object it was showing date with minus 5:30hr like below
enter image description here

and when I add offset to date and put cursor on that object it was showing date with plus 5:30hr like below

NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];

enter image description here

and when am trying to log that object it was showing exact date like below

enter image description here

Why its happening like this? Is this problem with timezone? Thanks in advance.

Ganesh G
  • 1,991
  • 1
  • 24
  • 37
  • Your actual date is correct. Just NSLog show NSDate in +0000 GMT without you current GMT offset. – Leta0n Aug 16 '13 at 07:19

3 Answers3

1

NSDate always comes as a GMT. So you have to format the date as per your required timeZone.. You can set your device timeZOne or can use Calendar to set the timeZOne..

Krishna Kumar
  • 1,652
  • 9
  • 17
0

NSDate is a "raw" date. That's why it is in GMT and your local and default time zone is GMT +5. It's up to the code to use NSDateFormatter to output the date to a value that makes sense for the user. 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]];
Community
  • 1
  • 1
geekchic
  • 2,371
  • 4
  • 29
  • 41
0

NSDate description return string representation in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from GMT.

If you want currentLocale, Use:

[date descriptionWithLocale:[NSLocale currentLocale]]
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144