JS时间工具

/** 
 * 时间工具
 * @author bamboo
 */
 export default {

    /** 补零 */
    addZero : function(num) {
        return num < 10 ? '0' + num : num;
    },

    /** 获取两位数的月份 */
    getMonthByMM : function(date) {
        return this.addZero(date.getMonth() + 1);
    },

    /** 获取两位数的日 */
    getDayByDD : function(date){
        return this.addZero(date.getDate());
    },
    /** 获取两位数的小时 */
    getHourseByHH : function(date) {
        return this.addZero(date.getHours())
    },
    /** 获取两位数的分钟 */
    getMinutesByMI : function(date) {
        return this.addZero(date.getMinutes());
    },
     /** 获取两位数的分钟 */
     getSecondsBySS : function(date) {
        return this.addZero(date.getSeconds());
    },

     /** 获取当前时间 */
     getYYYYMM : function(date) {
        return date.getFullYear() 
        + "-" + this.getMonthByMM(date);
    },

    /** 获取当前时间 */
    getYYYYMMDD : function(date) {
        return this.getYYYYMM(date) + "-" + this.getDayByDD(date);
    },
     /** 获取当前时间 */
     getHHMISS : function(date) {
        return this.getHourseByHH(date) 
        + ":" + this.getMinutesByMI(date)
        + ":" + this.getSecondsBySS(date);
    },

    /** 获取当前时间 */
    getYYYYMMDDHHMISS : function(date) {
        return this.getYYYYMMDD(date) + " " + this.getHHMISS(date);
    },

    /** 根据时间格式获取时间 支持常用格式 */
    getDateDateByFormat : function(format) {
        let date = new Date();
        let dateStr = '';
        switch(format) {
            case 'yyyy-MM-dd HH:mm:ss' :
                dateStr = this.getYYYYMMDDHHMISS(date);
                break; 
            case 'YYYY-MM-DD' :
                dateStr = getYYYYMMDD(date);
                break;
            case 'YYYY-MM' :
                dateStr = getYYYYMM(date);
                break;
            case 'YYYY' :
                dateStr = date.getFullYear();
                break;
            case 'YYYY/MM/DD' :
                dateStr = date.getFullYear() 
                    + "/" + this.getMonthByMM(date) 
                    + "/" + this.getDayByDD(date);
                break;
            default:;
        }
        return dateStr;
    }
    
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hi竹子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值