|
@@ -120,23 +120,51 @@ export default class StringUtils
|
|
|
args = String(num.toFixed(fixed));
|
|
|
return (args + unit);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- private static DayTime:number=24*60*60*1000;
|
|
|
- private static HoursTime:number=60*60*1000;
|
|
|
- private static MinutesTime:number=60*1000;
|
|
|
private static SecondsTime:number=1000;
|
|
|
+ private static MinutesTime:number=60*1000;
|
|
|
+ private static HoursTime:number=60*60*1000;
|
|
|
+ private static DayTime:number=24*60*60*1000;
|
|
|
+ private static MonthTime:number=30*24*60*60*1000;
|
|
|
+ private static YearTime:number=365*30*24*60*60*1000;
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 时间格式化
|
|
|
* @param time
|
|
|
- * @param daySeparator
|
|
|
- * @param hoursSeparator
|
|
|
- * @param minutesSeparator
|
|
|
- * @param secondsSeparator
|
|
|
+ * @param yearSeparator 年
|
|
|
+ * @param monthSeparator 月
|
|
|
+ * @param daySeparator 天
|
|
|
+ * @param hoursSeparator 时
|
|
|
+ * @param minutesSeparator 分
|
|
|
+ * @param secondsSeparator 秒
|
|
|
*/
|
|
|
- public static TimeFormatting(time:number,daySeparator:string="d",hoursSeparator:string="h",minutesSeparator:string="m",secondsSeparator:string="s"):string{
|
|
|
+ public static TimeFormatting(time:number,yearSeparator:string="y",monthSeparator:string="m",daySeparator:string="d",hoursSeparator:string="h",minutesSeparator:string="m",secondsSeparator:string="s"):string{
|
|
|
let timeStr:string="";
|
|
|
let index:number=0;
|
|
|
+ //年
|
|
|
+ let Year:number=0;
|
|
|
+ while(time>this.YearTime){
|
|
|
+ time-=this.YearTime;
|
|
|
+ Year++;
|
|
|
+ }
|
|
|
+ if(Year>0){
|
|
|
+ timeStr+=Year+yearSeparator;
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ //月
|
|
|
+ let Month:number=0;
|
|
|
+ while(time>this.MonthTime){
|
|
|
+ time-=this.MonthTime;
|
|
|
+ Month++;
|
|
|
+ }
|
|
|
+ if(Month>0){
|
|
|
+ if(Month<10){
|
|
|
+ timeStr+="0"+monthSeparator;
|
|
|
+ }else{
|
|
|
+ timeStr+=Month+monthSeparator;
|
|
|
+ }
|
|
|
+ index++;
|
|
|
+ }
|
|
|
//天
|
|
|
let Day:number=0;
|
|
|
while (time>this.DayTime) {
|
|
@@ -152,44 +180,54 @@ export default class StringUtils
|
|
|
index++;
|
|
|
}
|
|
|
//时
|
|
|
- let Hours:number=0;
|
|
|
- while (time>this.HoursTime) {
|
|
|
- time-=this.HoursTime;
|
|
|
- Hours++;
|
|
|
- }
|
|
|
- if(Hours>0){
|
|
|
- if(Hours<10){
|
|
|
- timeStr+="0"+Hours+hoursSeparator;
|
|
|
- }else{
|
|
|
- timeStr+=Hours+hoursSeparator;
|
|
|
+ if(index<2){
|
|
|
+ let Hours:number=0;
|
|
|
+ while (time>this.HoursTime) {
|
|
|
+ time-=this.HoursTime;
|
|
|
+ Hours++;
|
|
|
}
|
|
|
- index++;
|
|
|
+ if(Hours>0){
|
|
|
+ if(Hours<10){
|
|
|
+ timeStr+="0"+Hours+hoursSeparator;
|
|
|
+ }else{
|
|
|
+ timeStr+=Hours+hoursSeparator;
|
|
|
+ }
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return timeStr;
|
|
|
}
|
|
|
//分
|
|
|
- let Minute:number=0;
|
|
|
- while (time>this.MinutesTime) {
|
|
|
- time-=this.MinutesTime;
|
|
|
- Minute++;
|
|
|
- }
|
|
|
- if(Minute>0){
|
|
|
- if(Minute<10){
|
|
|
- timeStr+="0"+Minute+minutesSeparator;
|
|
|
- }else{
|
|
|
- timeStr+=Minute+minutesSeparator;
|
|
|
+ if(index<2){
|
|
|
+ let Minute:number=0;
|
|
|
+ while (time>this.MinutesTime) {
|
|
|
+ time-=this.MinutesTime;
|
|
|
+ Minute++;
|
|
|
}
|
|
|
- index++;
|
|
|
+ if(Minute>0){
|
|
|
+ if(Minute<10){
|
|
|
+ timeStr+="0"+Minute+minutesSeparator;
|
|
|
+ }else{
|
|
|
+ timeStr+=Minute+minutesSeparator;
|
|
|
+ }
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return timeStr;
|
|
|
}
|
|
|
//秒
|
|
|
- let Seconds:number=0;
|
|
|
- while (time>this.SecondsTime) {
|
|
|
- time-=this.SecondsTime;
|
|
|
- Seconds++;
|
|
|
- }
|
|
|
- if(Seconds>0&&index<2){
|
|
|
- if(Seconds<10){
|
|
|
- timeStr+="0"+Seconds+secondsSeparator;
|
|
|
- }else{
|
|
|
- timeStr+=Seconds+secondsSeparator;
|
|
|
+ if(index<2){
|
|
|
+ let Seconds:number=0;
|
|
|
+ while (time>this.SecondsTime) {
|
|
|
+ time-=this.SecondsTime;
|
|
|
+ Seconds++;
|
|
|
+ }
|
|
|
+ if(Seconds>0){
|
|
|
+ if(Seconds<10){
|
|
|
+ timeStr+="0"+Seconds+secondsSeparator;
|
|
|
+ }else{
|
|
|
+ timeStr+=Seconds+secondsSeparator;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return timeStr;
|