js – 时间格式化

function formatDate(date, pattern) {
    if(typeof pattern == 'undefined'){
        pattern = 'yyyy-MM-dd';
    }
    var str = '';
    if (date instanceof Date) {
        console.log('yes date')
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        if (month <= 9) {
            month = '0' + month;
        }
        var day = date.getDate();
        if (day <= 9) {
            day = '0' + day;
        }
        str = pattern.replace('yyyy', year).replace('MM', month).replace('dd', day);
    } else {
        console.log('no date')
    }
    return str;
}

发表评论

邮箱地址不会被公开。 必填项已用*标注