1.日期格式转为日期标准字符串:2015-03-19
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;
return y + '-' + m + '-' + d;
};
2.js方法返回值:2015-03-19 12:00:00
var formatDateTime = 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;
};
3.时间戳转为日期格式
//时间戳转日期格式
var formatDateTime3 = function(time, format){
var t = new Date(time);
var tf = function(i){return (i < 10 ? '0' : '') + i};
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
switch(a){
case 'yyyy':
return tf(t.getFullYear());
break;
case 'MM':
return tf(t.getMonth() + 1);
break;
case 'mm':
return tf(t.getMinutes());
break;
case 'dd':
return tf(t.getDate());
break;
case 'HH':
return tf(t.getHours());
break;
case 'ss':
return tf(t.getSeconds());
break;
}
})
};
4.时间格式字符串转为时间戳(毫秒)
var time1=‘2016-01-01 17:22:37’;
var date=new Date(time1.replace(/-/g, '/')); //开始时间
var time2=date.getTime(); 123
如何将2015-03-12 12:00 转换成标准时间()?
// 调用:parserDate("2015-03-19 12::00:00")
var parserDate = function (date) {
var t = Date.parse(date);
if (!isNaN(t)) {
return new Date(Date.parse(date.replace(/-/g, "/")));
} else {
return new Date();
}
};
本文来自 _let 的CSDN 博客 ,全文地址请点击:
https://blog.csdn.net/qq_39759115/article/details/78893853
网友评论