utils封装 —— 时间格式化

export class Util {
    /**
     * 格式化时间年月日时分秒
     * @param str
     */
    static formatTime( str:string){
        if( str===null || str==="" || str==="undefined"){
            return "--"
        }else{
            let date = new Date(str);
            let hours = date.getHours().toString();
            if(hours.length<2){
                hours = '0'+hours;
            }
            let minnutes = date.getMinutes().toString();
            if(minnutes.length<2){
                minnutes = '0'+minnutes;
            }
            let seconds = date.getSeconds().toString();
            if(seconds.length<2){
                seconds = '0'+seconds;
            }
            let getMonth = (date.getMonth()+1).toString();
            if(getMonth.length<2){
                getMonth = '0'+getMonth;
            }
            let getDate = date.getDate().toString();
            if(getDate.length<2){
                getDate = '0'+getDate;
            }
            return date.getFullYear() +'-'+ getMonth +'-'+ getDate + " " + hours+":"+minnutes+":"+seconds;
        }
    }

    /**
     * 格式化时分秒
     * @param {string} str
     * @return {string}
     */
    static formatHour(str:string){
        let date = new Date(str);
        let hours = date.getHours().toString();
        if(hours.length<2){
            hours = '0'+ hours;
        }
        let minutes = date.getMinutes().toString();
        if(minutes.length<2){
            minutes = '0'+minutes;
        }
        let seconds = date.getSeconds().toString();
        if( seconds.length<2){
            seconds = '0'+ seconds;
        }
        return hours+":"+minutes+":"+seconds;
    }
    /**
     * 格式化年月日
     * @param {string} str
     * @return {string}
     */
    static formatDay(str:string){
        let date = new Date(str);
        let getMonth = (date.getMonth() + 1).toString();
        if(getMonth.length<2){
            getMonth = '0'+getMonth;
        }
        let getDate = date.getDate().toString();
        if( getDate.length<2){
            getDate = '0'+getDate;
        }
        return date.getFullYear()+'-'+getMonth+'-'+getDate;
    }

    /**
     * 格式化时间
     * @param {object} date
     */
    static format(value){
        if(value<10){
            return '0'+value;
        }else{
            return value;
        }
    }

    static getDay(date){
        return `${date.getFullYear()}-${this.format(date.getMonth() + 1)}-${this.format(date.getDate())}`
    }

    static getTime(date){
        return `${this.format(date.getHours())}:${this.format(date.getMinutes())}:${this.format(date.getSeconds())}`
    }

    static getNowTime(date){
        return `${date.getFullYear()}-${this.format(date.getMonth() + 1)}-${this.format(date.getDate())} ${this.format(date.getHours())}:${this.format(date.getMinutes())}:${this.format(date.getSeconds())}`
    }

    /**
     * 判断是否是闰年
     */

    static isYear(year:number){
        return (year % 100 === 0) ? ( (year % 400 === 0) ? true : false):( ( year % 4 === 0) ? true : false );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值