携手创作,一起生长!这是我参与「日新计划 8 月更文应战」的第20天,点击查看活动概况
- 事例1: 小程序订单小票
- 事例2:订单概况时刻格局化
I. 事例1: 小程序订单小票
需求:小程序订单打印模板优化,头部增加配送订单、自提订单;自提订单增加显现自提时刻,配送订单无需显现。
订单接口数据格局:
"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];
}
II 事例2:订单概况时刻格局化
- 20210330105712->2021-03-30 10:57:12
同理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_];// 格局化时刻戳
III 代码完成
+ (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;
}
IV 前置知识
4.1 指定时区
为了保证都是东八区的时刻,格局化时设置东八时区。
// 处理时刻
- (NSString *)timeStampConversionNSString:(NSString *)timeStamp
{
//
//咱们也能够规则必定运用某一个时区:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy.MM.dd HH:mm:ss"];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];//东八区时刻
//这样不论咱们的手机是在哪里,打印出来的时刻都是东八区的时刻
// formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Tokyo"];//东九区时刻
// formatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];//零区时刻
// formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];//零区时刻,和GMT相同
// NSString *dateStr = [formatter stringFromDate:date];
NSLog(@"字符串时刻 = %@", dateStr);
// 假如包括下午,则去除
NSString *tmpdate= [formatter stringFromDate:date];
tmpdate = [tmpdate stringByReplacingOccurrencesOfString:@"下午" withString:@""];
tmpdate = [tmpdate stringByReplacingOccurrencesOfString:@"上午" withString:@""];
return tmpdate;
}
4.2 NSDate转NSString
NSDate->NSString
+ (NSString *)date4YYMMDD:(NSDate *)date{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd"];
return [formatter stringFromDate:date];
}
+ (NSString *)date4YYMM:(NSDate *)date{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM"];
return [formatter stringFromDate:date];
}