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"
以上就是Date()日期格式转换实例代码的详细内容,更多请关注其它相关文章!