千家信息网

怎么利用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处理时间戳并计算时间的差值"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。

0