js 获取当前时间并格式化

方式一

/**
 * 获取系统当前时间并格式化
 * @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;
}

方式二

见文末推荐

方式三

// 初始化日期
var today = new Date();
var yyyy = today.getFullYear();
var mm = String(today.getMonth() + 1).padStart(2, '0');
var dd = String(today.getDate()).padStart(2, '0');
var dd2 = String(today.getDate() + 7).padStart(2, '0');

$('#startDate').val(yyyy + "-" + mm + "-" + dd);
$('#endDate').val(yyyy + "-" + mm + "-" + dd2);

 

写在最后

  哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

 相关推荐:

posted @ 2018-02-07 11:06  Marydon  阅读(1490)  评论(0编辑  收藏  举报