V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
chouti
V2EX  ›  iDev

一个关于NSDateFormatter的问题

  •  
  •   chouti · 2013-02-06 23:49:47 +08:00 · 2783 次点击
    这是一个创建于 4068 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假设 date1 为 今天的日期,[NSDate date]

    我有如下代码:

    NSLocale *zhLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:zhLocale];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];

    NSString *dateString = [formatter stringFromDate:date1];
    self.tableViewCell.detailTextLabel.text = dateString;

    从以上代码我可以得到那个dateString为:「2013年2月6日 星期三」这样的字符串。

    然后我再尝试着将 dateString 转换回 NSDate 时:

    NSDate *date2 = [formatter dateFromString:dateString];

    这时的date2 我期望是再转换成当前日期。可是不知道为什么新的日期变成了1999年的12月28日。

    不知道是因为什么原因造成的,如果把 setDateStyle 的 NSDateFormatterFullStyle 改成 NSDateFormatterLongStyle 就没有这个问题。
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5296 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 09:20 · PVG 17:20 · LAX 02:20 · JFK 05:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.