var D:Date = StringToDate.parse("2011-03-04 15:22:21"); var d:Date = StringToDate.parse("2011-03-09 13:22:21"); showTimerTxt(d,D); function showTimerTxt(d:Date,D:Date):void { var time:Number=(d.getTime()-D.getTime())/1000/60/60/24; trace(d.getTime()-D.getTime()); if (time >= 1) { _timerTxt.text = "在" + Math.floor(time) + "天前"; } else { var temp:Number = time * 24; if (temp >= 1) { _timerTxt.text = "在" + Math.floor(temp) + "小时前"; } else { _timerTxt.text = "在" + Math.floor(temp * 60) + "分钟前"; } } }
package { public class StringToDate { public function StringToDate() { } public static function parse(dateString:String):Date { if (dateString == null) { return null; } if (dateString.indexOf("0000-00-00") != -1) { return null; } dateString = dateString.split("-").join("/"); return new Date(Date.parse( dateString )); } public static function transform(time:uint, stringMode:Boolean=false):String { if (stringMode) { var hours:uint = getHours(time); var minutes:uint = getMinutes(time); var seconds:uint = getSecond(time); return (hours < 10?"0" + hours:hours) + ":" + (minutes < 10?"0" + minutes:minutes) + ":" + (seconds < 10?"0" + seconds:seconds); } return getHours(time)+"h"+getMinutes(time)+"m"+getSecond(time)+"s"; } public static function getHours(needSecond:uint):uint { return uint(needSecond / (60 * 60)); } public static function getMinutes(needSecond:uint):uint { return uint(needSecond % (60 * 60) / 60 ); } public static function getSecond(needSecond:uint):uint { return uint(needSecond % (60 * 60) % 60); } } }