iOS将数字时间转换成英文时间

iOS将数字时间转换成英文时间
不知道有没有专门的转换方法,借用龙同学的方法先记记

@interface HHViewController : BUIRootViewController
@property (nonatomic, strong) NSDateFormatter *dateFormatter;   //.h文件中声明
@end

.m文件中

+ (NSString *)monthEn:(NSInteger)month{
   NSCalendar *caldendar = [NSCalendar currentCalendar];// 获取日历
   NSArray *monthArr = [NSArray arrayWithArray:caldendar.shortMonthSymbols];  // 获取日历月数组
   return monthArr[month - 1];  // 获得数字月份下的对应英文月缩写
}

比较懒,就借用项目中的一段代码

// dateArray中存储的是类似2016-10-15
 NSArray *dateArray = [[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:article.publishTime]]   componentsSeparatedByString:@"-"];  // article和article.publishTime是我自己的一个model以及它的属性publishTime[要修改]
            NSString *day = dateArray[2];
            NSString *month = dateArray[1];
            self.dateLabel.text = [NSString stringWithFormat:@"%@ %d",[HHViewController monthEn:[month intValue]],[day intValue]];   // 通过上面的类方法获得对应的英文时间:Oct 15

NSDateFormatter格式说明:
G: 公元时代,例如AD公元
yy: 年的后2位
yyyy: 完整年
MM: 月,显示为1-12
MMM: 月,显示为英文月份简写,如 Jan
MMMM: 月,显示为英文月份全称,如 Janualy
dd: 日,2位数表示,如02
d: 日,1-2位显示,如 2
EEE: 简写星期几,如Sun
EEEE: 全写星期几,如Sunday
aa: 上下午,AM/PM
H: 时,24小时制,0-23
K:时,12小时制,0-11
m: 分,1-2位
mm: 分,2位
s: 秒,1-2位
ss: 秒,2位
S: 毫秒

常用日期结构:
yyyy-MM-dd HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyyy

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容