页面时间格式化

2025-01-03 16:59:30
推荐回答(1个)
回答1:

function formatDate(formatStr, fdate)
{
var fTime, fStr = 'ymdhis';
if (!formatStr)
formatStr= "y-m-d h:i:s";
if (fdate)
fTime = new Date(fdate);
else
fTime = new Date();
var formatArr = [
fTime.getFullYear().toString(),
(fTime.getMonth()+1).toString(),
fTime.getDate().toString(),
fTime.getHours().toString(),
fTime.getMinutes().toString(),
fTime.getSeconds().toString()
]
for (var i=0; i {
formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
}
return formatStr;
}
var now = new Date();
var nowStr = formatDate("y-m-d h:i:s",now);
$("#nowTime").text(nowStr);