【问题标题】:Formatting an NSDate with Abbreviated Month/Day Names使用缩写的月/日名称格式化 NSDate
【发布时间】:2013-08-12 21:36:16
【问题描述】:

如果我有一个NSDate 对象,我如何格式化一个 NSString 以仅输出一天的前 3 个字母(如周一、周二、周三、周四、周五、周六、周日)和前 3 个字母月份(一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、十二月)?

目前我正在使用这个NSDateFormatter

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        dateFormatter.dateFormat = @"MMMM dd yyyy";

        NSDate *date = ...
        NSString *dateString = [dateFormatter stringFromDate:date];

        NSLog(@"dateString: %@",dateString);
        // outputs August 09 2013
        // I would like the output to be Fri, August 9

编辑

我也尝试过使用 NSDateComponent 但我想知道是否有一些获取缩写的捷径...

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];

【问题讨论】:

  • 您查看日期格式说明符了吗?见unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
  • @rmaddy 谢谢先生。不知道文档存在。从现在开始会参考它。
  • 查看NSDateFormatter 的参考文档。有一个指向“数据格式化程序”的链接,该链接又具有一个指向“日期格式化程序”的链接,最后有一个指向我引用的页面的链接。这真的很明显;)

标签: ios objective-c nsstring


【解决方案1】:

根据 rmaddy 提供的链接,@"EEE, MMM d"; 是解决方案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-03
    • 2013-03-02
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多