格式化日期和时间

格式化日期和时间

function getDate(dt) {
    //年
    var year = dt.getFullYear();
    //月
    var month = dt.getMonth();
    //日
    var date = dt.getDate();
    //时
    var hour = dt.getHours();
    //分
    var minute = dt.getMinutes();
    //秒
    var second = dt.getSeconds();

    month = month < 10? "0" + month: month;
    date = date < 10? "0" + date: date;
    hour = hour < 10? "0" + hour: hour;
    minute = minute < 10? "0" + minute: minute;
    second = second < 10? "0" +second: second;  

    return year + "年" + month + "月" + date + "日"  + hour + ":" + minute + ":" + second
}
console.log(getDate(new Date()));

你可能感兴趣的:(格式化日期和时间)