0

我有一些问题NSDate

我的代码如下所示:

NSDate *date    = [NSDate dateWithTimeIntervalSinceNow:logout];

当我在此代码处放置断点并将鼠标移到上方时,它显示为

2013-01-28 18:25:27 SGT

但是什么时候NSLog约会

我明白了:

2013-01-28 10:25:27

为什么会这样?如何显示正确的?

4

3 回答 3

2

这个问题只是因为NSDate 是一个“原始”日期。这就是为什么它在格林威治标准时间

你可以试试这个

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];

这将为您提供当前时区

于 2013-01-28T09:43:44.513 回答
1

NSDate 定义了一个对象,该对象表示具有某些属性(例如时区)的日期。如果你想显示一个日期,你可以选择只显示它的年份,也许只显示小时和分钟(所有这些都可以使用NSDateFormatter类来完成)。您正在做的是在 LLDB 上打印对象 - 这可能向您显示日期的 UTC 版本,所以我并不感到惊讶的是同一实例的调试器摘要显示不同的时间。

决定你需要用这个日期做什么,相应地设置它的时区,格式化它并在你的控制台中打印出来。

于 2013-01-28T09:32:03.993 回答
0
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);
于 2014-09-05T10:31:57.960 回答