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);
本文来自博客园,作者:Marydon,转载请注明原文链接:https://www.cnblogs.com/Marydon20170307/p/8425667.html
浙公网安备 33010602011771号