mongo 时间格式化 查询_MongoDB中查询转换(将时间戳转变通用日期格式)
在mongodb中日期保存的是long形的,但是打印出來不好看,需要在查詢的時候做一定的轉化,代碼如下:
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;
}
db.getCollection('state').find({"isall":"1"}).sort({"st":-1}).forEach(function (a) { a["st"] = (new Date(a["st"]).Format("yyyy-MM-dd"));a["ut"] = (new Date(a["ut"]).Format("yyyy-MM-dd")); printjson(a) })
核心是需要利用forEach去遍歷。
總結
以上是生活随笔為你收集整理的mongo 时间格式化 查询_MongoDB中查询转换(将时间戳转变通用日期格式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python入门第一课_入门第一课 Py
- 下一篇: find linux 目录深度_linu