DateFromString返回错误的值

时间:2012-11-19 12:57:19

标签: xcode macos nsdate nsdateformatter

我正在使用NSDateFormatter并且datefromString是整整一年

NSString *date    = [[command substringWithRange:NSMakeRange(pos.location + 3, command.length - pos.location - 9)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSLog(@"  date        [%@]   " , date       ); 
NSLog(@"  lastAccess  [%@]   " , lastAccess );
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss Z"]; 
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]]; 
NSDate *filedate = [dateFormat dateFromString:date];

NSLog(@"1     %@   %f  " , filedate    , [filedate   timeIntervalSince1970]); 
NSLog(@"2     %@   %f  " , lastAccess  , [lastAccess timeIntervalSince1970]); 
NSLog(@"3     %@   " , date);

产生以下输出

  date        [2012-11-18 19:00:23 +0000]   
  lastAccess  [2012-11-18 19:00:21 +0000]   
  1     2011-11-18 19:00:23 +0000   1321642823.000000  
  2     2012-11-18 19:00:21 +0000   1353265221.929860  
  3     2012-11-18 19:00:23 +0000   

日期有2012年再次确认3)

但请查看1)它有2011年 然而1)提交dateofomstring of date

有什么想法吗?

提前致谢

1 个答案:

答案 0 :(得分:2)

今年使用小写字母。

您的日期格式应为yyyy-MM-dd HH:mm:ss Z

根据unicode date format guide

  • yyyy表示年份
  • YYYY表示基于“年度周”的日历中的年份。

我不确定为什么会产生不同的结果,但您应该几乎总是使用小写变体。