欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

用JavaScript获取并格式化当前时间

最编程 2024-01-20 11:07:42
...
/**
* 获取系统当前时间并格式化
* @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;
}

推荐阅读