js 输出当前日期时间 yyyy-MM-dd HH:mm:ss 格式

/**
 * 生成yyyy-MM-dd HH:mm:ss 时间字符串
 */
function getDatetime() {
    let now = new Date;
    let year = now.getFullYear();
    let mounth = toDubleDigit(now.getMonth() + 1);
    let day = toDubleDigit(now.getDate());
    let hours = toDubleDigit(now.getHours());
    let minutes = toDubleDigit(now.getMinutes());
    let seconds = toDubleDigit(now.getSeconds());
    return year + "-" + mounth + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}

/**
 * 补齐两位数
 */
function toDubleDigit(digit) {
    return digit <= 9 ? ‘0‘ + digit : digit;
}


getDatetime();