将日期从[NSDate日期]缩短几个小时

将日期从[NSDate日期]缩短几个小时

我在用

NSDate *date = [NSDate date];

为了得到日期,但我得到的日期是两个小时。


潇潇雨雨
浏览 462回答 3
3回答

墨色风雨

NSDate对象没有时区。它们代表了一个绝对的时刻。然而,当你问一个description(用NSLog(),它必须选择一个时区。最合理的“默认”选择是格林尼治标准时间。如果你自己不在格林尼治时间,根据你自己的偏移量,日期似乎是不正确的。您应该始终使用NSDateFormatter若要创建用于显示的字符串,请执行以下操作。格式化程序的时区应该设置为您的,这是默认的。

拉莫斯之舞

你可以这样改正你的约会:NSDate * dateGMT = [NSDate date];NSTimeInterval secondsFromGMT = [[NSTimeZone localTimeZone] secondsFromGMT]; NSDate * correctDate = [dateGMT dateByAddingTimeInterval:secondsFromGMT];

qq_花开花谢_0

-(NSDate *)getDateInCurrentSystemTimeZone{     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];     return destinationDate;}
打开App,查看更多内容
随时随地看视频慕课网APP