按照指定格式返回时间戳
//需求:
//1,一分钟之内的显示“刚刚”
//2,一分钟以上且一个小时之内的,显示“多少分钟前”,例如“5分钟前”
//3,1小时以内的字颜色为黄色(以上三点与目前一样)
//4,1小时以上三天以内的显示“今天/昨天/前天+具体时间”
//5,前天以后的显示"日期+具体时间",如"2月11日 20:19"
//数据前提:
//1, 提供一个时间串(貌似是这样叫的吧)
//方法调用:
//1, [类名 timeStrTotimeStamp:时间串];
//方法来源:
//1, 通过网络代码进行改造
//1,一分钟之内的显示“刚刚”
//2,一分钟以上且一个小时之内的,显示“多少分钟前”,例如“5分钟前”
//3,1小时以内的字颜色为黄色(以上三点与目前一样)
//4,1小时以上三天以内的显示“今天/昨天/前天+具体时间”
//5,前天以后的显示"日期+具体时间",如"2月11日 20:19"
//数据前提:
//1, 提供一个时间串(貌似是这样叫的吧)
//方法调用:
//1, [类名 timeStrTotimeStamp:时间串];
//方法来源:
//1, 通过网络代码进行改造
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
|
//按照指定格式返回时间 (日期+具体时间",如"2月11日 20:19")
+(
NSString
*)speTimewithTimeStr:(
NSString
*)timeStr{
NSDateFormatter
*formatter = [[[
NSDateFormatter
alloc
]init]autorelease];
[formatter
setDateFormat
:
@"MM月dd日 HH:mm:ss"
];
NSTimeInterval
timeInterval = [timeStr
intValue
];
NSDate
*confromTimesp = [
NSDate
dateWithTimeIntervalSince1970
:timeInterval];
NSString
*str = [formatter
stringFromDate
:confromTimesp];
return
str;
}
//返回时间所在日期当天的准确时间. (20:19)
+(
NSString
*)smallTimewithTimeStr:(
NSString
*)timeStr{
NSDateFormatter
*formatter = [[[
NSDateFormatter
alloc
]init]autorelease];
[formatter
setDateFormat
:
@" HH:mm:ss"
];
NSTimeInterval
timeInterval = [timeStr
intValue
];
NSDate
*confromTimesp = [
NSDate
dateWithTimeIntervalSince1970
:timeInterval];
NSString
*str = [formatter
stringFromDate
:confromTimesp];
return
str;
}
//判断两个日期是否是同一天
+(
BOOL
)isSameDay:(NSDate*)date
1
date2
:(NSDate*)date
2
{
NSCalendar* calendar = [
NSCalendar
currentCalendar
];
unsigned unitFlags =
NSYearCalendarUnit
|
NSMonthCalendarUnit
|
NSDayCalendarUnit
;
NSDateComponents* comp
1
= [calendar
components
:unitFlags
fromDate
:date
1
];
NSDateComponents* comp
2
= [calendar
components
:unitFlags
fromDate
:date
2
];
return
[comp
1
day
] == [comp
2
day
] &&
[comp
1
month
] == [comp
2
month
] &&
[comp
1
year
] == [comp
2
year
];
}
//时间转时间戳
+(
NSString
*)timeStrTotimeStamp:(
id
)timeStr{
//获取当前时间时间串
//与获得时间串进行对比
//根据时间差显示不同内容
//返回内容
int
nowTime = [[
DSUtils
timeStamp
]
intValue
];
int
gapTime = nowTime - [timeStr
intValue
];
NSString
*timeStamp =
nil
;
if
(gapTime <
6
0
) {
//一分钟内显示刚刚
timeStamp = [
NSString
stringWithFormat
:
@"刚刚"
];
}
else
if
(
6
0
<=gapTime && gapTime<
6
0
*
6
0
){
//一分钟以上且一个小时之内的,显示“多少分钟前”,例如“5分钟前”
timeStamp = [
NSString
stringWithFormat
:
@"%i分钟前"
,gapTime/
6
0
];
}
else
if
(
6
0
*
6
0
<=gapTime && gapTime<
6
0
*
6
0
*
2
4
*
3
){
//1小时以上三天以内的显示“今天/昨天/前天+具体时间”
NSString
*dayStr ;
int
gapDay = gapTime/(
6
0
*
6
0
*
2
4
) ;
switch
(gapDay) {
case
0
:
{
//在24小时内,存在跨天的现象. 判断两个时间是否在同一天内.
NSDate
*date
1
= [
NSDate
date
];
NSTimeInterval
timeInterval = [timeStr
intValue
];
NSDate
*date
2
= [
NSDate
dateWithTimeIntervalSince1970
:timeInterval];
BOOL
idSameDay = [
DSUtils
isSameDay
:date
1
date2
:date
2
];
if
(idSameDay ==
YES
) {
dayStr = [
NSString
stringWithFormat
:
@"今天"
];
}
else
{
dayStr = [
NSString
stringWithFormat
:
@"昨天"
];
}
}
break;
case
1
:
dayStr = [
NSString
stringWithFormat
:
@"昨天"
];
break;
case
2
:
dayStr = [
NSString
stringWithFormat
:
@"前天"
];
break;
default
:
break;
}
// timeStamp = [dayStr stringByAppendingString:[DSUtils smallTimewithTimeStr:timeStrs]];
timeStamp = [
NSString
stringWithFormat
:
@"%@%@"
,dayStr,[
DSUtils
smallTimewithTimeStr
:timeStr]];
}
else
{
//前天以后的显示"日期+具体时间",如"2月11日 20:19"
timeStamp = [
NSString
stringWithString
:[
DSUtils
speTimewithTimeStr
:timeStr]];
}
return
[timeStamp
copy
];
}
|