转换成Date()默认格式
var parserDate = function (date) {
var t = Date.parse(date);
if (!isNaN(t)) {
return new Date(Date.parse(date.replace(/-/g, "/")));
} else {
return new Date();
}
};
parserDate("2017-06-10 13:59:46");
//Sat Jun 10 2017 13:59:46 GMT+0800 (中国标准时间)
转换成常用格式
var formatDate = function (date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h< 10 ? ('0' + h) : h;
var minute = date.getMinutes();
minute = minute < 10 ? ('0' + minute) : minute;
var second=date.getSeconds();
second=second<10?('0'+second):second;
return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};
var a=new Date();
formatDate (a);
//"2017-06-10 14:02:27"
Atas ialah kandungan terperinci Date()日期格式转换实例代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!