如何利用moment处理时间戳并计算时间的差值_第1页
如何利用moment处理时间戳并计算时间的差值_第2页
如何利用moment处理时间戳并计算时间的差值_第3页
如何利用moment处理时间戳并计算时间的差值_第4页
如何利用moment处理时间戳并计算时间的差值_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第如何利用moment处理时间戳并计算时间的差值项目使用nodejs写服务端,有个功能就是统计代理服务器流量,然后把统计的数据通过echarts渲染到页面。

当然统计数据这里用到了定时器,在使用的是

var

schedule=

require(

'node-schedule');

有兴趣的同学可以在npm上搜一搜关于js定时任务的事,其实都大同小异,差不多都是运用corn表达式。

以下是我的定时从代理服务器获取数据并存库。

schedule.scheduleJob('*/15*****',function(){

console.log('timer!!!!!!!!!!');

vardataObj1={};

iplists.forEach(function(ele,index){

varreq=http.request("http://"+ele+":14567/stat",function(res){

dataObj1.time=newDate(res.headers.date);

dataObj1.ip=req.getHeader("host").split(":")[0];

res.setEncoding('utf-8');

vartempData='';

res.on('data',function(chunk){

tempData+=chunk;

varresultObj=JSON.parse(tempData);

dataObj1.flow=resultObj.bw15s;

varflow1=newflowrank1({

ip:dataObj1.ip,

flow:dataObj1.flow,

time:newDate(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');//settoJanuary1st,12:00amthisyear

moment().startOf('month');//settothefirstofthismonth,12:00am

moment().startOf('quarter');//settothebeginningofthecurrentquarter,1stdayofmonths,12:00am

moment().startOf('week');//settothefirstdayofthisweek,12:00am

moment().startOf('isoWeek');//settothefirstdayofthisweekaccordingtoISO8601,12:00am

moment().startOf('day');//setto12:00amtoday

moment().startOf('date');//setto12:00amtoday

moment().startOf('hour');//settonow,butwith0mins,0secs,and0ms

moment().startOf('minute');//settonow,butwith0secondsand0milliseconds

moment().startOf('second');//sameasmoment().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);

vara=moment([2008,9]);

varb=moment([2007,0]);

a.diff(b,'years');//1

a.diff(b,'years',true);//1.75

moment().add(Number,String);

moment().add(Duration);

moment().add(Object);

varmoment=require('moment');

varstarttime=moment(moment.unix(parseInt(req.query.starttime)).toDate());

console.log("==============");

console.log(moment(moment.unix(parseInt(req.query.starttime)).toDate()));

varendtime=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);

returnres.end(JSON.stringify(retcode.operateDbErr));

varresult=retcode.res_ok;

result.data=doc;

console.log(doc)

res.end(JSON.stringify(result));

}elseif(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);

returnres.end(JSON.stringify(retcode.operateDbErr));

varresult=retcode.res_ok;

result.data=doc;

console.log(doc)

res.end(JSON.stringify(result));

}elseif(endtime.diff(starttime,'months')=1){

console.log("inflowrank3");

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);

returnres.end

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论