as3 時間格式工具

CnctSoft發表於2019-05-11

public class DateUtil
	{
		public static function getUnixTimestamp():int{
			return new Date().time/1000;
		}
		
		/**
		 * 將日期格式化成YYYY-MM-DD格式 
		 * @param date Date
		 * @return str String
		 * 
		 */		
		public static function formatDateYYYYMMDD(date:Date):String
		{
			var fullYear:String = date.fullYear.toString();
			var month:String = date.month+1>9 ? (date.month+1).toString() : "0"+(date.month+1).toString();
			var _date:String = date.date>9 ? date.date.toString() : "0" + date.date.toString();
			return fullYear + "-" + month + "-" + _date;
		}
		
		/**
		 * 將日期格式化成YYYY-MM-DD HH:JJ:SS格式 
		 * @param date
		 * @return 
		 * 
		 */		
		public static function formatDate(date:Date):String
		{
			var fullYear:String = date.fullYear.toString();
			var month:String = date.month+1>9 ? (date.month+1).toString() : "0"+(date.month+1).toString();
			var _date:String = date.date>9 ? date.date.toString() : "0" + date.date.toString();
			var hours:String = date.hours > 9 ? date.hours.toString() : "0" + date.hours.toString();
			var minutes:String = date.minutes>9 ? date.minutes.toString() : "0" + date.minutes.toString();
			var seconds:String = date.seconds > 9 ? date.seconds.toString() : "0" + date.seconds.toString();
			return fullYear + "-" + month + "-" + _date + " " + hours + ":" + minutes + ":" + seconds;
		}
		
		public static function formatDateHHMMSS(date:Date):String
		{
			var hours:String = date.hours > 9 ? date.hours.toString() : "0" + date.hours.toString();
			var minutes:String = date.minutes>9 ? date.minutes.toString() : "0" + date.minutes.toString();
			var seconds:String = date.seconds > 9 ? date.seconds.toString() : "0" + date.seconds.toString();
			return hours + ":" + minutes + ":" + seconds;
		}
		
		public static function formatDateHHMMSS_SSS(date:Date):String
		{
			var hours:String = date.hours > 9 ? date.hours.toString() : "0" + date.hours.toString();
			var minutes:String = date.minutes>9 ? date.minutes.toString() : "0" + date.minutes.toString();
			var seconds:String = date.seconds > 9 ? date.seconds.toString() : "0" + date.seconds.toString();
			var mill:String = date.milliseconds.toString();
			if(date.milliseconds < 10)
				mill = "00" + date.milliseconds;
			else if(date.milliseconds < 100)
				mill = "0" + date.milliseconds;
			return hours + ":" + minutes + ":" + seconds + "." + mill;
		}
		
		public function DateUtil()
		{
		}
	}



內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。

相關文章