欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS时间字符串格式化输出技巧详解

程序员文章站 2023-12-19 23:58:28
一.前言 最近项目开发过程中用到了大量的关于时间的处理,将后台返回的时间字符串转换为指定的格式时间再显示在ui上. 例如: 将后台返回的时间字符串2017-04-16...

一.前言

最近项目开发过程中用到了大量的关于时间的处理,将后台返回的时间字符串转换为指定的格式时间再显示在ui上.

例如: 将后台返回的时间字符串2017-04-16 13:08:06转换为:2017年04月16日、2017年04月、04月16日、2017-04-16、2017-04、04-16、13:08、星期几等等.

项目是多人开发,由于前期没有统一处理时间转换的问题,后期发现项目中好多关于时间转换的代码,大部分都是通过(- : 等字符)截取成字符串数组再取相应时间拼接成指定格式,输出在ui显示的地方,代码非常的臃肿,并且这种方式非常不可取.

原因:后台返回的时间字符串 并不都是 2017-04-16 13:08:06这种格式,还有2017-04-16这种格式,截取前需要长度格式等校验,多了很多校验代码.非常不可取.

既然是时间,我们便要通过时间的思维来完成转换问题,不要通过截取字符串的方式

于是我便写了一个类,来统一处理转换问题.

二.效果

iOS时间字符串格式化输出技巧详解

具体怎么操作:

三.将时间字符串->nsdate

首先我们要将2017-04-16 13:08:06或2017-04-16这种格式时间字符串转换为nsdate

我们新建一个nsdate的category,笔者取名为nsdate+xhcategory,写一个时间字符串->nsdate方法,代码如下:

+(nsdate*)xh_datewithformat_yyyy_mm_dd_hh_mm_ss_string:(nsstring *)string
{
  nsdateformatter* dateformat = [[nsdateformatter alloc] init];
  [dateformat setdateformat:@"yyyy-mm-dd hh:mm:ss"];
  nsdate *date =[dateformat datefromstring:string];
  return date;
}

为了兼用其他格式时间字符串,我们把可能的情况都写上,如下

+(nsdate *)xh_datewithformat_yyyy_mm_dd_hh_mm_string:(nsstring *)string
{
  nsdateformatter* dateformat = [[nsdateformatter alloc] init];
  [dateformat setdateformat:@"yyyy-mm-dd hh:mm"];
  nsdate *date =[dateformat datefromstring:string];
  return date;
}

+(nsdate *)xh_datewithformat_yyyy_mm_dd_hh_string:(nsstring *)string
{
  nsdateformatter* dateformat = [[nsdateformatter alloc] init];
  [dateformat setdateformat:@"yyyy-mm-dd hh"];
  nsdate *date =[dateformat datefromstring:string];
  return date;
}

+(nsdate *)xh_datewithformat_yyyy_mm_dd_string:(nsstring *)string
{
  nsdateformatter* dateformat = [[nsdateformatter alloc] init];
  [dateformat setdateformat:@"yyyy-mm-dd"];
  nsdate *date =[dateformat datefromstring:string];
  return date;
}

+(nsdate *)xh_datewithformat_yyyy_mm_string:(nsstring *)string
{
  nsdateformatter* dateformat = [[nsdateformatter alloc] init];
  [dateformat setdateformat:@"yyyy-mm"];
  nsdate *date =[dateformat datefromstring:string];
  return date;
}

再写一个统一转换时间字符串为 nsdate的方法,如下:

+(nsdate *)xh_datewithdatestring:(nsstring *)datestring
{
  nsdate *date = nil;
  date = [self xh_datewithformat_yyyy_mm_dd_hh_mm_ss_string:datestring];
  if(date) return date;
  date = [self xh_datewithformat_yyyy_mm_dd_hh_mm_string:datestring];
  if(date) return date;
  date = [self xh_datewithformat_yyyy_mm_dd_hh_string:datestring];
  if(date) return date;
  date = [self xh_datewithformat_yyyy_mm_dd_string:datestring];
  if(date) return date;
  date = [self xh_datewithformat_yyyy_mm_string:datestring];
  if(date) return date;
  return nil;
}

四.将nsdate -> nsdateformatter

为什么要再转换为nsdateformatter,有些人可能已经明白了,我们点开nsdateformatter可以看到nsdateformatter有以下属性

@property (nullable, copy) nscalendar *calendar ns_available(10_7, 4_0);
@property (nullable, copy) nstimezone *timezone ns_available(10_7, 4_0);
@property nsinteger era;
@property nsinteger year;
@property nsinteger month;
@property nsinteger day;
@property nsinteger hour;
@property nsinteger minute;
@property nsinteger second;
@property nsinteger nanosecond ns_available(10_7, 5_0);
@property nsinteger weekday;
@property nsinteger weekdayordinal;
@property nsinteger quarter ns_available(10_6, 4_0);
@property nsinteger weekofmonth ns_available(10_7, 5_0);
@property nsinteger weekofyear ns_available(10_7, 5_0);
@property nsinteger yearforweekofyear ns_available(10_7, 5_0);
@property (getter=isleapmonth) bool leapmonth ns_available(10_8, 6_0);
@property (nullable, readonly, copy) nsdate *date ns_available(10_7, 4_0);

@end

我们新建一个nsdatecomponents 的category,笔者取名nsdatecomponents+xhcategory,并实现如下方法:

+(nsdatecomponents *)xh_datecomponentsfromdate:(nsdate *)date
{
  nsdatecomponents *components = [[nscalendar currentcalendar] components:nscalendarunityear| nscalendarunitmonth | nscalendarunitday | nscalendarunitweekofyear | nscalendarunithour | nscalendarunitminute | nscalendarunitsecond | nscalendarunitweekday | nscalendarunitweekdayordinal fromdate:date];
  return components;

}

接着我们就可以进行转换操作了,我们新建一个nsstring的category,笔者取名nsstring+xhdateformat

在nsstring+xhdateformat.h文件中写上需要转换的类型如下:

/**
 * x年x月x日
 */
@property(nonatomic,copy,readonly)nsstring *xh_formatnianyueri;

/**
 * x年x月
 */
@property(nonatomic,copy,readonly)nsstring *xh_formatnianyue;

/**
 * x月x日
 */
@property(nonatomic,copy,readonly)nsstring *xh_formatyueri;

/**
 * x年
 */
@property(nonatomic,copy,readonly)nsstring *xh_formatnian;

/**
 * x时x分x秒
 */
@property(nonatomic,copy,readonly)nsstring *xh_formatshifenmiao;

/**
 * x时x分
 */
@property(nonatomic,copy,readonly)nsstring *xh_formatshifen;

/**
 * x分x秒
 */
@property(nonatomic,copy,readonly)nsstring *xh_formatfenmiao;

/**
 * yyyy-mm-dd
 */
@property(nonatomic,copy,readonly)nsstring *xh_format_yyyy_mm_dd;

/**
 * yyyy-mm
 */
@property(nonatomic,copy,readonly)nsstring *xh_format_yyyy_mm;

/**
 * mm-dd
 */
@property(nonatomic,copy,readonly)nsstring *xh_format_mm_dd;

/**
 * yyyy
 */
@property(nonatomic,copy,readonly)nsstring *xh_format_yyyy;

/**
 * hh:mm:ss
 */
@property(nonatomic,copy,readonly)nsstring *xh_format_hh_mm_ss;

/**
 * hh:mm
 */
@property(nonatomic,copy,readonly)nsstring *xh_format_hh_mm;

/**
 * mm:ss
 */
@property(nonatomic,copy,readonly)nsstring *xh_format_mm_ss;

#pragma mark - 转换为星期几
@property(nonatomic,copy,readonly)nsstring *xh_formatweekday;

在 nsstring+xhdateformat.m 实现如下:

-(nsstring *)xh_formatnianyueri
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%ld年%02ld月%02ld日",date.year,date.month,date.day];
}
-(nsstring *)xh_formatnianyue
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%ld年%02ld月",date.year,date.month];
}
-(nsstring *)xh_formatyueri
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%02ld月%02ld月",date.month,date.day];
}
-(nsstring *)xh_formatnian
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%ld年",date.year];
}
-(nsstring *)xh_formatshifenmiao
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%ld时%02ld分%02ld秒",date.hour,date.minute,date.seconds];
}
-(nsstring *)xh_formatshifen
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%ld时%02ld分",date.hour,date.minute];
}
-(nsstring *)xh_formatfenmiao
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%02ld分%02ld秒",date.minute,date.seconds];
}
-(nsstring *)xh_format_yyyy_mm_dd
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%ld-%02ld-%02ld",date.year,date.month,date.day];
}
-(nsstring *)xh_format_yyyy_mm
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%ld-%02ld",date.year,date.month];
}
-(nsstring *)xh_format_mm_dd
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%02ld-%02ld",date.month,date.day];
}
-(nsstring *)xh_format_yyyy
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%ld",date.year];
}
-(nsstring *)xh_format_hh_mm_ss
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%02ld:%02ld:%02ld",date.hour,date.minute,date.seconds];
}
-(nsstring *)xh_format_hh_mm
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%02ld:%02ld",date.hour,date.minute];
}
-(nsstring *)xh_format_mm_ss
{
  nsdate *date = [nsdate xh_datewithdatestring:self];
  return [nsstring stringwithformat:@"%02ld:%02ld",date.minute,date.seconds];
}

-(nsstring *)xh_formatweekday
{
  nsstring *weekstr=nil;
  nsdate *date = [nsdate xh_datewithdatestring:self];
  switch (date.weekday) {
    case 2:
      weekstr = @"星期一";
      break;
    case 3:
      weekstr = @"星期二";
      break;
    case 4:
      weekstr = @"星期三";
      break;
    case 5:
      weekstr = @"星期四";
      break;
    case 6:
      weekstr = @"星期五";
      break;
    case 7:
      weekstr = @"星期六";
      break;
    case 1:
      weekstr = @"星期天";
      break;
    default:
      break;
  }
  return weekstr;
}

五.调用:

  self.timestring = @"2017-04-16 13:08:06";

  //星期
  nsstring *time0 = self.timestring.xh_formatweekday;

  //2017年04月16日
  nsstring *time1 = self.timestring.xh_formatnianyueri;

  //2017年04月
  nsstring *time2 = self.timestring.xh_formatnianyue;

  //04月16日
  nsstring *time3 = self.timestring.xh_formatyueri;

  //2017年
  nsstring *time4 = self.timestring.xh_formatnian;

  //13时08分01秒
  nsstring *time5 = self.timestring.xh_formatshifenmiao;

  //13时08分
  nsstring *time6 = self.timestring.xh_formatshifen;

  //08分01秒
  nsstring *time7 = self.timestring.xh_formatfenmiao;

  //2017-04-16
  nsstring *time8 = self.timestring.xh_format_yyyy_mm_dd;

  //2017-04
  nsstring *time9 = self.timestring.xh_format_yyyy_mm;

  //04-16
  nsstring *time10 = self.timestring.xh_format_mm_dd;

  //2017
  nsstring *time11 = self.timestring.xh_format_yyyy;

  //13:08:06
  nsstring *time12 = self.timestring.xh_format_hh_mm_ss;

  //13:08
  nsstring *time13 = self.timestring.xh_format_hh_mm;

  //08:06
  nsstring *time14 = self.timestring.xh_format_mm_ss;

github地址https://github.com/coderzhuxh/xhdate

完整项目下载地址:xhdate_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: