【问题标题】:How can one display Gregorian calendar year for an NSDate when calendar in iOS settings is Japanese or Buddhist当 iOS 设置中的日历为日语或佛教时,如何显示 NSDate 的公历年
【发布时间】:2013-10-07 19:11:47
【问题描述】:

当日历设置为日语时,我正在尝试显示 NSDate 的 4 位公历年份。

NSLog(@"Date joined: %@", self.dateJoined);
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.calendar = calendar;
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
formatter.dateFormat = @"yyyy";
NSLog(@"Year joined: %@", [formatter stringFromDate:self.dateJoined]);

日志:

Date joined: 2012-05-29 22:13:54 +0000
Year joined: 4000

我做错了什么?

【问题讨论】:

  • 你试过“en_US_POSIX”而不是“en_US”吗?
  • @MartinR 我刚试了一下,结果是一样的。

标签: ios objective-c nsdate nsdateformatter


【解决方案1】:

试试这个。它有效。

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComps = [cal components:(NSYearCalendarUnit) fromDate:self. dateJoined];

NSLog(@"date = %d",[dateComps year]);

// IGNORE IF USING ARC
[cal release];

【讨论】:

    猜你喜欢
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2019-12-03
    • 1970-01-01
    相关资源
    最近更新 更多