objective-c计算相对于现在的时间差
2012-03-09 00:00
120 查看
最近做了一个小应用程序,是读取新浪微博的。微博上面对于新发的微博,不是告诉你具体什么时候发布的,而是告诉你几秒钟之前,几分钟之前,几个小时之前之类的相对于现在的时间。可以使用下面代码来计算这个时间差。
-
(NSString
*
)timestamp
{
//
Calculate distance time string
//
time_t now;
time(
&
now);
int
distance
=
(
int
)difftime(now, createdAt);
if
(distance
<
) distance
=
;
if
(distance
<
60
) {
self.timestamp
=
[NSString stringWithFormat:
@"
%d %s
"
, distance, (distance
==
1
)
?
"
second ago
"
:
"
seconds ago
"
];
}
else
if
(distance
<
60
*
60
) {
distance
=
distance
/
60
;
self.timestamp
=
[NSString stringWithFormat:
@"
%d %s
"
, distance, (distance
==
1
)
?
"
minute ago
"
:
"
minutes ago
"
];
}
else
if
(distance
<
60
*
60
*
24
) {
distance
=
distance
/
60
/
60
;
self.timestamp
=
[NSString stringWithFormat:
@"
%d %s
"
, distance, (distance
==
1
)
?
"
hour ago
"
:
"
hours ago
"
];
}
else
if
(distance
<
60
*
60
*
24
*
7
) {
distance
=
distance
/
60
/
60
/
24
;
self.timestamp
=
[NSString stringWithFormat:
@"
%d %s
"
, distance, (distance
==
1
)
?
"
day ago
"
:
"
days ago
"
];
}
else
if
(distance
<
60
*
60
*
24
*
7
*
4
) {
distance
=
distance
/
60
/
60
/
24
/
7
;
self.timestamp
=
[NSString stringWithFormat:
@"
%d %s
"
, distance, (distance
==
1
)
?
"
week ago
"
:
"
weeks ago
"
];
}
else
{
static
NSDateFormatter
*
dateFormatter
=
nil;
if
(dateFormatter
==
nil) {
dateFormatter
=
[[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
}
NSDate
*
date
=
[NSDate dateWithTimeIntervalSince1970:createdAt];
self.timestamp
=
[dateFormatter stringFromDate:date];
}
return
timestamp;
}
原文链接:
http://www.cnblogs.com/zhuqil/archive/2011/04/29/2033028.html
-
(NSString
*
)timestamp
{
//
Calculate distance time string
//
time_t now;
time(
&
now);
int
distance
=
(
int
)difftime(now, createdAt);
if
(distance
<
) distance
=
;
if
(distance
<
60
) {
self.timestamp
=
[NSString stringWithFormat:
@"
%d %s
"
, distance, (distance
==
1
)
?
"
second ago
"
:
"
seconds ago
"
];
}
else
if
(distance
<
60
*
60
) {
distance
=
distance
/
60
;
self.timestamp
=
[NSString stringWithFormat:
@"
%d %s
"
, distance, (distance
==
1
)
?
"
minute ago
"
:
"
minutes ago
"
];
}
else
if
(distance
<
60
*
60
*
24
) {
distance
=
distance
/
60
/
60
;
self.timestamp
=
[NSString stringWithFormat:
@"
%d %s
"
, distance, (distance
==
1
)
?
"
hour ago
"
:
"
hours ago
"
];
}
else
if
(distance
<
60
*
60
*
24
*
7
) {
distance
=
distance
/
60
/
60
/
24
;
self.timestamp
=
[NSString stringWithFormat:
@"
%d %s
"
, distance, (distance
==
1
)
?
"
day ago
"
:
"
days ago
"
];
}
else
if
(distance
<
60
*
60
*
24
*
7
*
4
) {
distance
=
distance
/
60
/
60
/
24
/
7
;
self.timestamp
=
[NSString stringWithFormat:
@"
%d %s
"
, distance, (distance
==
1
)
?
"
week ago
"
:
"
weeks ago
"
];
}
else
{
static
NSDateFormatter
*
dateFormatter
=
nil;
if
(dateFormatter
==
nil) {
dateFormatter
=
[[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
}
NSDate
*
date
=
[NSDate dateWithTimeIntervalSince1970:createdAt];
self.timestamp
=
[dateFormatter stringFromDate:date];
}
return
timestamp;
}
原文链接:
http://www.cnblogs.com/zhuqil/archive/2011/04/29/2033028.html
相关文章推荐
- objective-c计算相对于现在的时间差
- objective-c计算相对于现在的时间差
- 【转载】iOS 计算某个时间点与现在的时间差
- 利用时间戳来准确计算某个时间点具现在的时间差(转)
- JS计算从某一日期到现在的时间差
- 利用时间戳来准确计算某个时间点具现在的时间差
- 利用时间戳来准确计算某个时间点具现在的时间差
- iOS 计算两个时间的时间差
- 工具类:计算距离现在的时间:刚刚/几分钟前/几小时前/几天前/几个月前/几年前
- java计算Date类时间差的函数
- Javascript时间差计算函数
- C# 中时间差计算
- 计算指定时间与当前的时间差
- C#计算两个时间差的方法代码分享
- java计算时间差
- php计算两个时间差并返回差多少天、时、分、秒
- 计算两个时间的时间差 就是 计算执行一段程序使用的时间
- python 计算两个带日期的时间差
- Oracle 时间差计算
- PHP时间比较和时间差如何计算