服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS时间字符串格式化输出技巧详解

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

2021-03-13 16:40绿豆粥与茶叶蛋 IOS

本篇文章主要介绍了iOS时间格式化输出技巧,可以将后台返回的时间字符串转换为指定的格式时间再显示在UI上,有兴趣的可以了解一下。

一.前言

最近项目开发过程中用到了大量的关于时间的处理,将后台返回的时间字符串转换为指定的格式时间再显示在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方法,代码如下:

?
1
2
3
4
5
6
7
+(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;
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
+(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的方法,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
+(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有以下属性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@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,并实现如下方法:

?
1
2
3
4
5
6
+(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文件中写上需要转换的类型如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * 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 实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-(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;
}

五.调用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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

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

原文链接:http://www.jianshu.com/p/902b0c2cca17#

延伸 · 阅读

精彩推荐
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25