js获取当前时间,并格式化为"yyyy-MM-dd HH:mm:ss"

/**
 * Created by Administrator on 2019/11/15.
 *指尖敲打着世界 ----一个阳光而又不失帅气的少年!!!.
 */
// js获取当前时间,并格式化为"yyyy-MM-dd HH:mm:ss"
function getFormatDate() {
    var date = new Date();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentDate = date.getFullYear() + "-" + month + "-" + strDate
        + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
    return currentDate;
}

console.log(getFormatDate());//2019-11-15 15:26:49
//将时间戳转换成 yyyy-MM-dd HH:mm:ss
// putTime 参数是毫秒级时间戳

function formatDate(inputTime) {
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}

var time = new Date().getTime();

console.log(formatDate(time));//2019-11-15 15:26:49

 

posted @ 2019-11-15 15:34  指尖下的世界  阅读(1097)  评论(0编辑  收藏  举报
/* 看板娘 */ /*炸泡*/
/* 鼠标点击求赞文字特效 */