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

下載本文檔

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

文檔簡(jiǎn)介

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

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

var

schedule=

require(

'node-schedule');

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

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

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)在來(lái)展示需要根據(jù)前端傳過(guò)來(lái)的時(shí)間戳來(lái)篩選出數(shù)據(jù)的代碼,處理時(shí)間我用到了moment這個(gè)類庫(kù),基本包含了時(shí)間所有的處理方法。

總結(jié)以下moment的幾個(gè)常用的函數(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論