需要打印信息[NSDate date]

时间:2012-11-21 05:12:48

标签: ios nsdate

我打印日期时

//getting the current date and time
self.date = [NSDate date];
NSLog(@"%@",date);

我得到的日期是正确的,但是时间延迟了6小时。我的系统时间是正确的。

5 个答案:

答案 0 :(得分:16)

试试这个

NSLocale* currentLoc = [NSLocale currentLocale];
NSLog(@"%@",[[NSDate date] descriptionWithLocale:currentLoc]); 

答案 1 :(得分:10)

使用 NSDateFormatter

NSDate *today = [NSDate date];

//Create the dateformatter object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//Set the required date format
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

//Get the string date
NSString *dateString = [dateFormatter stringFromDate:today];

//Display on the console
NSLog(dateString);

答案 2 :(得分:2)

在调试器中记录NSDate有点误导,因为它为您提供特定时区的日历日期和时间 - UTC / GMT。然而,NSDate没有固有的时区或任何与人类如何看待和思考日期的固有关系。相反,它是一个时间戳。像NSDateComponentsNSTimeZoneNSDateFormatter等类都存在,以提供人工环境和格式。

所以你看到的是格式化为特定格式和UTC时区的时间戳,这是在调试器或控制台中打印时始终显示NSDate的方式。如果您要计算UTC与您自己的时区之间的时区偏移量,您会发现日期代表您给出的时间戳,而不是一个多少小时的时间戳。

答案 3 :(得分:0)

您可以设置当前时区以自定义日期格式。

此链接可以提供帮助: https://stackoverflow.com/a/7213629/456471

答案 4 :(得分:0)

默认日期字符串表示可能将日期格式化为UTC,而不是您的本地时区(它将使用的确切格式未定义,并且可能会在发行版之间发生更改,因此您不应该依赖它)。如果需要以特定格式(或使用特定时区,包括本地时区)格式化日期,则应使用NSDateFormatter类;有关详细信息,请参阅Data Formatting GuideNSDateFormatter Class Reference