ios – 需要打印信息[NSDate日期]

前端之家收集整理的这篇文章主要介绍了ios – 需要打印信息[NSDate日期]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我打印日期时
  1. //getting the current date and time
  2. self.date = [NSDate date];
  3. NSLog(@"%@",date);

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

解决方法

使用NSDateFormatter
  1. NSDate *today = [NSDate date];
  2.  
  3. //Create the dateformatter object
  4. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  5.  
  6. //Set the required date format
  7. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  8.  
  9. //Get the string date
  10. NSString *dateString = [dateFormatter stringFromDate:today];
  11.  
  12. //Display on the console
  13. NSLog(dateString);

猜你在找的iOS相关文章