JS将日期转换为yyyy-MM-dd HH:mm:ss

时间:2022-08-22 06:59:41
//格式化后日期为:yyyy-MM-dd HH:mm:ss
function FormatAllDate(sDate) {
var date = new Date(sDate);
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + ;
var strDate = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
//月
if (month >= && month <= ) {
month = "" + month;
}
//日
if (strDate >= && strDate <= ) {
strDate = "" + strDate;
}
//时
if (hours >= && hours <= ) {
hours = "" + hours;
}
//分
if (minutes >= && minutes <= ) {
minutes = "" + minutes;
}
//秒
if (seconds >= && seconds <= ) {
seconds = "" + seconds;
}
//格式化后日期为:yyyy-MM-dd HH:mm:ss
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + hours + seperator2 + minutes + seperator2 + seconds;
return currentdate;
}