js 获取当前时间并格式化

js 获取当前时间并格式化

CreateTime--2018年2月7日11:04:16

Author:Marydon

方式一

/**
 * 获取系统当前时间并格式化
 * @returns yyyy-MM-dd HH:mm:ss
 */
function getCurrentFormatDate() {
    // 系统当前时间格式化
    var currentFormatDate = "";
    // 获取系统当前日期
    var date = new Date();
    // 获取当前年
    var currentYear = date.getFullYear();
    // 获取当前月
    var currentMonth = date.getMonth() + 1;
    currentMonth = (currentMonth <= 9)?"0" + currentMonth:currentMonth;
    // 获取当前日
    var currentDay = date.getDate();
    currentDay = (currentDay <= 9)?"0" + currentDay:currentDay;
    // 时
    var currentHours = date.getHours();
    // 分
    var currentMinutes = date.getMinutes();
    // 秒
    var currentSeconds = date.getSeconds();
    // yyyy-MM-dd HH:mm:ss
    currentFormatDate = currentYear + "-" + currentMonth + "-" + currentDay + " " + currentHours + ":" + currentMinutes
            + ":" + currentSeconds;
    return currentFormatDate;
}

方式二

  见文末推荐

原文地址:https://www.cnblogs.com/Marydon20170307/p/8425667.html