js 原生格式化日期方法

代码如下

 formatDate: function(time) {
        var time = new Date(time);
        var Y = time.getFullYear();
        var M = getDate(time.getMonth() + 1);
        var d = getDate(time.getDate());
        var h = getDate(time.getHours());
        var m =getDate(time.getMinutes());
        // var s =getDate(time.getSeconds()); 
        timeStr = `${Y}-${M}-${d} ${h}:${m}`;
        return timeStr;
    },
    getDate: function(num) {
        if (num < 10) {
            return "0" + num;
        } else {
            return num;
        }
    }

获取星期

getDateTime: function(data) {
        var data = new Date(data).getDay();
        var week = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        return week[data];
    },

你可能感兴趣的:(js 原生格式化日期方法)