获取本地时间时,关于时间的显示格式问题

1.获取本地时间的相关代码:

NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString * date = [formatter stringFromDate:[NSDate date]];
NSLog(@"date = %@",date);

在显示时间的时候,如果表示小时的H为大写,则时间显示为24进制,如果表示小时的为小写则时间显示为12进制;

还有表示年份的y尽量小写,具体什么原因我也不是很清楚,因为我在这里吃过亏,年份大写有时得到的结果会相差一年。

2.关于时间显示的形式问题:

  加一句代码:

formatter.dateStyle = NSDateFormatterMediumStyle;

有这几种形式:

/*
    NSDateFormatterNoStyle = kCFDateFormatterNoStyle,
    NSDateFormatterShortStyle = kCFDateFormatterShortStyle,
    NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle,
    NSDateFormatterLongStyle = kCFDateFormatterLongStyle,
    NSDateFormatterFullStyle = kCFDateFormatterFullStyle
    */

对于显示什么形式则自己去体验。

原文地址:https://www.cnblogs.com/h-tao/p/5133338.html