在自己写的formatName基础上,写个,将时间转化为自定义的输出字符串格式信息

StringUtil.formatName在该文章中,含有声明及定义:

as3 formatName 功能实现


代码:

		/**
		 * 传入以Date为单位的时间,将转成对应的,时,分,秒单位的格式字符串表示<br/>
		 * 其中:{yyyy}、{mm}、{dd}、{HH}、{MM}、{SS}是关键匹配字符串,分别表示:年、月、日、时、分、秒
		 * */
		public static function toHHMMSSByDate(date:Date, format:String = "{yyyy}-{mm}-{dd} {HH}:{MM}:{SS}"):void{
			var year:String = "" + date.fullYear;
			
			var month:String = "" + (date.month + 1);
			month = (date.month + 1) < 10 ? "0" + month : month;
			
			var dayOfMonth:String = "" + (date.date);
			dayOfMonth = (date.date) < 10 ? "0" + dayOfMonth : dayOfMonth;
			
			var hours:String = "" + (date.hours);
			hours = (date.hours) < 10 ? "0" + hours : hours;
			
			var minutes:String = "" + (date.minutes);
			minutes = (date.minutes) < 10 ? "0" + minutes : minutes;
			
			var seconds:String = "" + (date.seconds);
			seconds = (date.seconds) < 10 ? "0" + seconds : seconds;
			
			return StringUtil.formatName(format, {yyyy:year, mm:month, dayOfMonth:dayOfMonth, HH:hours, MM:minutes, SS:seconds});
		}
		
		/**
		 * 传入以毫秒为单位的时间,将转成对应的,时,分,秒单位的格式字符串表示<br/>
		 * 其中:{HH}、{MM}、{SS}是关键匹配字符串,分别表示:时、分、秒
		 * */
		public static function toHHMMSSByMs(ms:int, format:String = "{HH}:{MM}:{SS}"):void{
			var s:int = ms / 1000;
			var hours:int = s / 3600;
			s %= 3600;
			var minutes:int = s / 60;
			s %= 60;
			
			var hoursStr:String = "" + (hours);
			hoursStr = (hours) < 10 ? "0" + hoursStr : hoursStr;
			
			var minutesStr:String = "" + (minutes);
			minutesStr = (minutes) < 10 ? "0" + minutesStr : minutesStr;
			
			var seconds:String = "" + (s);
			seconds = (s) < 10 ? "0" + seconds : seconds;
			
			return StringUtil.formatName(format, {HH:hoursStr, MM:minutesStr, SS:seconds});
		}
		
		/**
		 * 将毫秒转成:?时?分?秒
		 */		
		public static function toHMSByMs(ms:int):String{
			return toHMSByS(ms / 1000);
		}
		
		/**
		 * 将秒转成:?时?分?秒
		 */		
		public static function toHMSByS(s:int):String{
			var hours:int = s / 3600;
			s %= 3600;
			var minutes:int = s / 60;
			s %= 60;
			
			var hoursStr:String = "" + (hours);
			hoursStr = (hours) < 10 ? "0" + hoursStr : hoursStr;
			
			var minutesStr:String = "" + (minutes);
			minutesStr = (minutes) < 10 ? "0" + minutesStr : minutesStr;
			
			var seconds:String = "" + (s);
			seconds = (s) < 10 ? "0" + seconds : seconds;
			
			return hoursStr + "时" + minutesStr + "分" + seconds + "秒";
		}


你可能感兴趣的:(在自己写的formatName基础上,写个,将时间转化为自定义的输出字符串格式信息)