Reputation: 29064
I have some issues with NSDate
.
The code I have looks like:
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:logout];
When I put my breakpoint at this code and move my mouse over, it shows as
2013-01-28 18:25:27 SGT
but when NSLog
date
I get this:
2013-01-28 10:25:27
Why is this so? How do show the correct one?
Upvotes: 0
Views: 223
Reputation: 129
Try this it works!!
NSDate *localDate =[NSDate date]; // get the date
NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMT];
NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] + timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
NSLog(@"%@",gmtDate);
Upvotes: 0
Reputation: 14304
NSDate defines an object that represents a date with certain properties (such as timezone). If you wish to display a date, you could choose to show only it's year, maybe only the hours and minutes (all this can be done using the NSDateFormatter
class). What you're doing is printing the object out on the LLDB - this shows you probably the UTC version of the date so I'm not surprised that the debugger summary of that same instance shows a different time.
Decide what you need to do with this date, set it's timezone accordingly, format it and print it out in your console.
Upvotes: 1
Reputation: 7388
This problem is just because NSDate is a "raw" date. That's why it is in GMT
You can try this,
NSDate* sourceDate = [NSDate dateWithTimeIntervalSinceNow:logout];
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];
this will give you your current timezone
Upvotes: 2