js格式化输出mongo数据库时间
描述:
mongo数据库时间格式:2019-07-29T07:22:09.318Z
前台转换后的时间格式:2019-07-29 07:22:09
技术环境:
html,js,jQuery
bootstrap-table-master
mongo 4.0.10
js函数:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function changeDateFormat(cellval) { var dateVal = cellval + ""; console.log("dateVal:"+dateVal); if (cellval != null) { var reg=new RegExp(".\\d{3}\\+\\d{4}$"); var date = new Date(dateVal.replace(reg, "").replace("T", " ")); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{ field:"time", title:"日期", formatter: function (value, row, index) { return changeDateFormat(value); }
注:js正则表达式要用RegExp声明,不要直接定义为字符串
参考博客:https://www.cnblogs.com/xbq8080/p/7979149.html