【问题标题】:How to format date 12/2/31 H, 2:21:41 AM in Objective-c?如何在 Objective-c 中格式化日期 12/2/31 H、2:21:41 AM?
【发布时间】:2019-12-04 07:07:33
【问题描述】:

这就是我再次得到我们当前日期 2019 年的日期。

12/2/31 H, 2:21:41 AM

谁能帮助我如何在 Objective-c 中将此日期解析为 NSDate 对象?

【问题讨论】:

  • NSString 到 NSDate 的转换,可以参考这里stackoverflow.com/questions/7204748/…
  • @pqteru 尝试在您共享的链接中检查我的日期格式和日期格式。两者是不同的。现在我们还有 2019 年,但这里显示的是 31。所以我们也不知道它的日历类型。

标签: objective-c iphone nsdateformatter ios13


【解决方案1】:

这种日历格式属于日本日历。

要使用NSDateFormatter 解析日期,您必须将H 替换为Heisei,并将日本日历实例分配给日期格式化程序。

NSString *dateString = [@"12/2/31 H, 2:21:41 AM" stringByReplacingOccurrencesOfString:@"H" withString:@"Heisei"];
NSCalendar *japanese = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierJapanese];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US@calendar=japanese"];
formatter.calendar = japanese;
formatter.dateFormat = @"MM/d/yy G, h:mm:ss a";
NSDate *date = [formatter dateFromString:dateString];
NSLog(@"%@", date);

【讨论】:

    猜你喜欢
    • 2020-06-05
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    相关资源
    最近更新 更多