怎么利用moment处理时间戳并计算时间的差值
发表于:2024-10-25 作者:千家信息网编辑
千家信息网最后更新 2024年10月25日,这篇"怎么利用moment处理时间戳并计算时间的差值"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一
千家信息网最后更新 2024年10月25日怎么利用moment处理时间戳并计算时间的差值
这篇"怎么利用moment处理时间戳并计算时间的差值"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"怎么利用moment处理时间戳并计算时间的差值"文章吧。
项目使用nodejs写服务端,有个功能就是统计代理服务器流量,然后把统计的数据通过echarts渲染到页面。
当然统计数据这里用到了 定时器,在使用的是
var schedule = require( 'node-schedule');
有兴趣的同学可以在npm上搜一搜关于js定时任务的事,其实都大同小异,差不多都是运用corn表达式。
以下是我的 定时从代理服务器获取数据 并存库。
schedule.scheduleJob('*/15 * * * * * ', function () { console.log('timer !!!!!!!!!!'); var dataObj1 = {}; iplists.forEach(function (ele, index) { var req = http.request("http://" + ele + ":14567/stat", function (res) { dataObj1.time = new Date(res.headers.date); dataObj1.ip = req.getHeader("host").split(":")[0]; res.setEncoding('utf-8'); var tempData = ''; res.on('data', function (chunk) { tempData += chunk; var resultObj = JSON.parse(tempData); dataObj1.flow = resultObj.bw15s; var flow1 = new flowrank1({ ip: dataObj1.ip, flow: dataObj1.flow, time: new Date(dataObj1.time) }); flow1.save(function (err, flow1) { if (err) { console.log(err); return; } }); }); }); req.on("error", function (err) { console.log(err); }); req.end() }); });
现在来展示 需要根据前端传过来的 时间戳 来筛选出数据的代码,处理时间我用到了moment这个类库,基本包含了时间所有的处理方法。
总结以下moment的几个常用的函数:
moment().startOf('year'); // set to January 1st, 12:00 am this yearmoment().startOf('month'); // set to the first of this month, 12:00 ammoment().startOf('quarter'); // set to the beginning of the current quarter, 1st day of months, 12:00 ammoment().startOf('week'); // set to the first day of this week, 12:00 ammoment().startOf('isoWeek'); // set to the first day of this week according to ISO 8601, 12:00 ammoment().startOf('day'); // set to 12:00 am todaymoment().startOf('date'); // set to 12:00 am todaymoment().startOf('hour'); // set to now, but with 0 mins, 0 secs, and 0 msmoment().startOf('minute'); // set to now, but with 0 seconds and 0 millisecondsmoment().startOf('second'); // same as moment().milliseconds(0);moment().diff(Moment|String|Number|Date|Array);moment().diff(Moment|String|Number|Date|Array, String);moment().diff(Moment|String|Number|Date|Array, String, Boolean);var a = moment([2008, 9]);var b = moment([2007, 0]);a.diff(b, 'years'); // 1a.diff(b, 'years', true); // 1.75moment().add(Number, String);moment().add(Duration);moment().add(Object);
var moment = require('moment');var starttime = moment(moment.unix(parseInt(req.query.starttime)).toDate());console.log("==============");console.log(moment(moment.unix(parseInt(req.query.starttime)).toDate()));var endtime = moment(moment.unix(parseInt(req.query.endtime)).toDate());console.log(moment(moment.unix(parseInt(req.query.endtime)).toDate()));console.log(endtime.diff(starttime, 'hour'));console.log(endtime.diff(starttime, 'months'));console.log(endtime.diff(starttime, 'months'));/** * 查询小于1天的数据 */if (endtime.diff(starttime, 'hour') <= 24) { console.log("flowrank1"); flowrank1.find({ ip: req.query.ip, time: { $gt: moment.unix(req.query.starttime).toDate(), $lte: moment.unix(req.query.endtime).toDate() } }, { _id: 0, ip: 1, flow: 1, time: 1 }, function(err, doc) { if (err) { console.log("err!!!!!") console.log(err); return res.end(JSON.stringify(retcode.operateDbErr)); } var result = retcode.res_ok; result.data = doc; console.log(doc) res.end(JSON.stringify(result)); })} else if (endtime.diff(starttime, 'months') == 0) { console.log("flowrank2!!!!"); flowrank2.find({ ip: req.query.ip, time: { $gt: moment.unix(req.query.starttime).toDate(), $lte: moment.unix(req.query.endtime).toDate() } }, { _id: 0, ip: 1, flow: 1, time: 1 }, function(err, doc) { if (err) { console.log("err!!!!!") console.log(err); return res.end(JSON.stringify(retcode.operateDbErr)); } var result = retcode.res_ok; result.data = doc; console.log(doc) res.end(JSON.stringify(result)); })} else if (endtime.diff(starttime, 'months') >= 1) { console.log("in flowrank3"); flowrank3.find({ ip: req.query.ip, time: { $gt: moment.unix(req.query.starttime).toDate(), $lte: moment.unix(req.query.endtime).toDate() } }, { _id: 0, ip: 1, flow: 1, time: 1 }, function(err, doc) { if (err) { console.log("err!!!!!") console.log(err); return res.end(JSON.stringify(retcode.operateDbErr)); } var result = retcode.res_ok; result.data = doc; console.log(doc) res.end(JSON.stringify(result)); })} else { return res.end(JSON.stringify(retcode.res_err));}
以上就是关于"怎么利用moment处理时间戳并计算时间的差值"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
时间
处理
内容
数据
差值
服务
统计
就是
文章
服务器
知识
篇文章
代理
大同小异
差不多
一搜
代码
价值
任务
兴趣
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发工作要准备什么
清远市言梦网络技术有限公司
服务器能读取移动硬盘吗
审计局网络安全管理制度
莒南软件开发视频
组态软件开发收费
mirbase数据库怎么打开
服务器下载第三方软件
腾讯网络安全运营管理员工资
手机软件开发怎么设置声音
手机怎么解决谷歌服务器连接不上
网络安全法普及情况
阿里云服务器安全性如何
奉贤区什么是网络技术标准
oracle11g服务器
服务器租还是买
湛江oa软件开发方案
河北网络技术转让怎么样
方舟如何开免费服务器
网络安全自拟
杭州鲲鹏网络技术有限公司
在线办公网络安全的股票
自学计算机网络技术视频
我的世界迷失在服务器里的玩家
数据库与微信安全中心
数据库占用存储怎么计算
bjl软件开发
数据库dt2
八一学院考试数据库
管家婆sn和sql数据库