如何利用moment處理時間戳并計算時間的差值_第1頁
如何利用moment處理時間戳并計算時間的差值_第2頁
如何利用moment處理時間戳并計算時間的差值_第3頁
如何利用moment處理時間戳并計算時間的差值_第4頁
如何利用moment處理時間戳并計算時間的差值_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第如何利用moment處理時間戳并計算時間的差值項目使用nodejs寫服務端,有個功能就是統(tǒng)計代理服務器流量,然后把統(tǒng)計的數(shù)據(jù)通過echarts渲染到頁面。

當然統(tǒng)計數(shù)據(jù)這里用到了定時器,在使用的是

var

schedule=

require(

'node-schedule');

有興趣的同學可以在npm上搜一搜關(guān)于js定時任務的事,其實都大同小異,差不多都是運用corn表達式。

以下是我的定時從代理服務器獲取數(shù)據(jù)并存庫。

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

現(xiàn)在來展示需要根據(jù)前端傳過來的時間戳來篩選出數(shù)據(jù)的代碼,處理時間我用到了moment這個類庫,基本包含了時間所有的處理方法。

總結(jié)以下moment的幾個常用的函數(shù):

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天的數(shù)據(jù)

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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論