--- 函数
/**
* @param format* @param currDate
* @returns
* 转换时间格式
*/
function formatDate(format,currDate) {
/*
* eg:format="YYYY-MM-dd hh:mm:ss";
*/
var o = {
"M+" :currDate.getMonth() + 1, // month
"d+" :currDate.getDate(), // day
"h+" :currDate.getHours(), // hour
"m+" :currDate.getMinutes(), // minute
"s+" :currDate.getSeconds(), // second
"q+" :Math.floor((currDate.getMonth() + 3) / 3), // quarter
"S" :currDate.getMilliseconds()
// millisecond
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (currDate.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
for ( var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
: ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
调用--
var Currdate=formatDate("yyyy-MM-dd hh:mm:ss",new Date(时间戳变量));
alert(Currdate);
输出结果:2015-07-22 15:55:57
http://www.zhujiangroad.com/program/JavaScript/71601.html