欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

utils封装 —— 时间格式化

程序员文章站 2022-07-14 23:41:37
...
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 );
    }
}
相关标签: 时间格式化