您的位置:首页 > Web前端 > JavaScript

javascript转换日期字符串

2016-08-30 16:37 148 查看





<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="jquery-2.1.4.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
</body>
<script type="text/javascript">
var common = {};
//date转格式
Date.prototype.Format = function(fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
common.parseDate = function (dateStr){
if(dateStr=='')
return null;
var _dateStr = new String(dateStr).replace(/\-/g, '/').replace(/\.\d+$/, '');
var date = new Date(_dateStr);
if(date.toString() !== 'Invalid Date'){
return date;
}
return null;
};
var bzStartTime ="2016-08-31 00:00:00";
var a = common.parseDate(bzStartTime).Format("yyyy-MM-dd hh:mm:ss");
console.log(a)//2016-08-31 00:00:00
</script>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: