携手创造,共同成长!这是我参与「日新方案 8 月更文应战」的第17天,点击查看活动概况

引言

系统自带款式

格式 日期 时刻
NSDateFormatterNoStyle “” “”
NSDateFormatterShortStyle 2022/7/31 上午11:40
NSDateFormatterMediumStyle 2022年7月31日 上午11:40:29
NSDateFormatterLongStyle 2022年7月31日 GMT+8 上午11:40:49
NSDateFormatterFullStyle 2022年7月31日 星期三 中国标准时刻 上午11:41:16

自定义格式

  1. 事例1: 小程序订单小票

iOS小技能:自定义时间格式(适配iOS15.4之后12/24小时制的问题)

  1. 事例2:订单概况时刻格式化

iOS小技能:自定义时间格式(适配iOS15.4之后12/24小时制的问题)

I 自定义格式

HH:表明24小时制 hh: 表明12小时制

GGG: 公元时代,例如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
h:时,12小时制,0-11
m: 分,1-2位
mm: 分,2位
s: 秒,1-2位
ss: 秒,2位
S:毫秒
zzz:三位字符串表明“时区”(例如GMT)。缩写 Z

1.1 小程序订单小票

需求:小程序订单打印模板优化,头部添加配送订单、自提订单;自提订单添加显现自提时刻,配送订单无需显现。

iOS小技能:自定义时间格式(适配iOS15.4之后12/24小时制的问题)

订单接口数据格式:

      "selfMentionEndTime" : "2022-05-18 10:08:03",
      "selfMentionStartTime" : "2022-05-18 10:08:03",

实现:

    if(detaiModel.deliveryMethod.intValue == QCTOrderdeliveryMethod4To_the_shop_self_mention){//自提
        [command addSetJustification:0];// * @param n 左 中 右对齐,0左对齐,1中心对齐,2右对齐
        [command addPrintMode: 0x0];
        //自提时刻 YY- MM- DD hh:mm - hh:mm
        NSString *start = detaiModel.delivery.selfMentionStartTime;
//        时刻格式化
        start = [QCT_Common strdatedateFormat:@"yyyy-MM-dd HH:mm" fromDateFormat:@"yyyy-MM-dd HH:mm:ss" objstr:start];
        NSString *end = detaiModel.delivery.selfMentionEndTime;
        end = [QCT_Common strdatedateFormat:@"HH:mm" fromDateFormat:@"yyyy-MM-dd HH:mm:ss" objstr:end];
       [command addText:[NSString stringWithFormat:@"提货时刻:%@-%@\n",start,end]];
        [command addPrintAndLineFeed];
   }

1.2 订单概况时刻格式化

  • 20210330105712->2021-03-30 10:57:12

iOS小技能:自定义时间格式(适配iOS15.4之后12/24小时制的问题)
同理2020年01月01日转成2020-01-01 ,可利用NSDateFormatter先把2020年01月01日字符串转NSDate,再把NSDate转成2020-01-01 字符串

            payinfomodel.createTime  = [QCT_Common strdatedateFormat:@"yyyy-MM-dd HH:mm:ss" fromDateFormat:@"yyyyMMddHHmmss" objstr:refund_time_];// 格式化时刻戳

1.3 代码实现

+ (NSString *)strdatedateFormat:(NSString*)toDateFormat fromDateFormat:(NSString *)fromDateFormat objstr:(NSString*)objstr{
    NSString *tmpbirthday = @"";
    if(![NSStringQCTtoll isBlankString:objstr]){
        tmpbirthday = objstr;
        tmpbirthday  = [QCT_Common date4ateFormat:toDateFormat withdate:[QCT_Common dateWithFormatterString:tmpbirthday dateFormat:fromDateFormat]];
    }
    return tmpbirthday;
}
+ (NSString *)date4ateFormat:(NSString *)DateFormat withdate:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  //  [formatter setDateStyle:NSDateFormatterMediumStyle];
//    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:DateFormat];
    return [formatter stringFromDate:date];
}
+ (NSDate*)dateWithFormatterString:(NSString*)dateWithString dateFormat:(NSString*)dateFormat {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    dateFormatter.dateFormat = dateFormat;
    NSDate *date = [dateFormatter dateFromString:dateWithString];
    return date;
}

II iOS15.4之后12/24小时制的问题

2.1 问题

问题:假如没有设置locale和NSCalendarIdentifierISO8601,即便指定格式化指定了24小时制,也会跟随系统设置显现。

iOS小技能:自定义时间格式(适配iOS15.4之后12/24小时制的问题)

2.2 适配

处理方式:自定义时刻格式的时分,指定locale和NSCalendarIdentifierISO8601。

iOS小技能:自定义时间格式(适配iOS15.4之后12/24小时制的问题)

+ (NSString *)date:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateStyle:NSDateFormatterMediumStyle];
//    [formatter setTimeStyle:NSDateFormatterShortStyle];// 自带的款式
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    // 指定local,真机调试,转换时刻 需要设置 NSLocale
    NSLocale *zh_CNLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//zh_CN  en_US en_GB zh_Hans_CN
    formatter.locale =zh_CNLocale;
    formatter.calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];//东八区时刻
    //这样不论咱们的手机是在哪里,打印出来的时刻都是东八区的时刻
    //    formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Tokyo"];//东九区时刻
    //    formatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];//零区时刻
    //    formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];//零区时刻,和GMT一样
//    return [formatter stringFromDate:date];
    NSString *tmpdate=  [formatter stringFromDate:date];
    tmpdate = [tmpdate stringByReplacingOccurrencesOfString:@"下午" withString:@""];
    tmpdate = [tmpdate stringByReplacingOccurrencesOfString:@"上午" withString:@""];
    return tmpdate;

2.3 小结

养成指定locale的好习惯


+ (NSDateFormatter*)getzh_CNLocale{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSLocale *zh_CNLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//zh_CN  en_US en_GB zh_Hans_CN
    formatter.locale =zh_CNLocale;
    formatter.calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];//东八区时刻
    //这样不论咱们的手机是在哪里,打印出来的时刻都是东八区的时刻
    return formatter;
}

see alos

公众号:iOS逆向