现在的位置: 首页 > 综合 > 正文

as3.0 timestamp 转成相对时间 即几天前 几小时前形式

2013年10月09日 ⁄ 综合 ⁄ 共 992字 ⁄ 字号 评论关闭
如下 很简单
package tv.kuapp.bobby.pc.util{
	
	public class RelativeTimeUtil {
		//两种格式的timestamp都可以处理。Sun Oct 24 20:07:33 +0000 2010 格式 和1337301360 格式
//	public var myRelativeTime:String = timestampToRelative("Sun Oct 24 20:07:33 +0000 2010");
	public static function timestampToRelative(timestamp:Number):String {
	//--Parse the timestamp as a Date object--\\
	var pastDate:Date = new Date(timestamp*1000);//乘1000是因为timestamp是秒数,而这里参数需要毫秒数
	//--Get the current data in the same format--\\
	var currentDate:Date = new Date();//毫米数
	//--seconds inbetween the current date and the past date--\\
	var secondDiff:Number = (currentDate.getTime() - pastDate.getTime())/1000;
	//--Return the relative equavalent time--\\
	switch (true) {
		case secondDiff < 60 :
			return int(secondDiff) + '秒前';
			break;
		case secondDiff < 120 :
			return '1分钟前';
			break;
		case secondDiff < 3600 :
			return int(secondDiff / 60) + '分钟前';
			break;
		case secondDiff < 86400 :
			return int(secondDiff / 3600) + '小时前';
			break;
		case secondDiff < 172800 :
			return '昨天';
			break;
		case secondDiff < 2592000:
			return int(secondDiff / 86400) + '天前';
			break;
		default :
			return int(secondDiff / 2592000) + '月前';
			break;
	}}
}
}

抱歉!评论已关闭.