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 · Feb 6, 2013 · 4051 views
    This topic created in 4990 days ago, the information mentioned may be changed or developed.
    假设 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 就没有这个问题。
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2586 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 04:29 · PVG 12:29 · LAX 21:29 · JFK 00:29
    ♥ Do have faith in what you're doing.