Moment.js 计算时间差

理论知识:Moment.js 日期处理类库

实现效果:使用momentjs中的方法计算时间差。

第1步:获取当前日期时间;

第2步:计算两者时间差。

官方链接:momentjs官方链接

实际代码:

//第1步,获取当前日期
let formattedDate = moment().format('YYYY-MM-DD HH:mm:ss');

//第2步,计算时间差
var a = moment(formattedDate);
var b = moment(apiResData);
var timeDiff =  a.diff(b, 'years', true); // 1.75

备注:

apiResData可以是任意之,数据格式是:2024-05-16 16:14:32

写到这儿就完成了,之后是使用的加强版。

实现效果:对数组接口返回的所有数据,筛选时间属性值。时间范围有4个:1年以内、1~2年、2~3年、3年以上。筛选au满足其中一个乃至于多个时间范围。

效果图:

核心代码:

var res = data.data;
      let formattedDate = moment().format('YYYY-MM-DD HH:mm:ss');
      var a = moment(formattedDate);
      var b 
      var timeDiff
      var cLength=[
        { 
          index:0,
          title: '1年内',
          minVal:0,
          maxVal:1,
          color: '#000',
          bgColor:'rgba(1,255,1,.7)',
          layerGroupName:'测量时间类型',
          isgroup:'yes',
          notCreate: false,
          mutexGroup: 'tc_Depth',
          checked: true,
          i18n_text: 'tc_MeasuringTimePlane_0Year',
        },
        { 
          index:3,
          title: '1~2年',
          minVal:1,
          maxVal:2,
          color: '#000',
          bgColor: 'rgba(255,255,0,.7)',
          layerGroupName:'测量时间类型',
          isgroup:'yes',
          notCreate: false,
          mutexGroup: 'tc_Depth',
          checked: true,
          i18n_text: 'tc_MeasuringTimePlane_1Year',
        },
        { 
          index:2,
          title: '2~3年',
          minVal:2,
          maxVal:3,
          color: '#000',
          bgColor: 'rgba(255,192,0,.7)',
          layerGroupName:'测量时间类型',
          isgroup:'yes',
          notCreate: false,
          mutexGroup: 'tc_Depth',
          checked: true,
          i18n_text: 'tc_MeasuringTimePlane_2Year',
        },
        { 
          index:1,
          title: '3年以上',
          minVal:3,
          maxVal:100,
          color: '#fff',
          bgColor: 'rgba(214,20,21,.7)',
          layerGroupName:'测量时间类型',
          isgroup:'yes',
          notCreate: false,
          mutexGroup: 'tc_Depth',
          checked: true,
          i18n_text: 'tc_MeasuringTimePlane_3Year',
        },
      ]
      // 添加年份筛选
      var filteredRes = res.filter(function (v) {
          b = moment(v.surend);
          timeDiff = a.diff(b, 'years', true); 
          return cLength.some(function(range) {
            return _.inRange(timeDiff, range.minVal, range.maxVal);
          });
      });
console.log('添加年份筛选',filteredRes);

 

其它,接口数据如下:

//接口请求到的数据
[
    {
        "guid": "8d0357b9-58f8-4fa0-9a00-949c29d3b1e2",
        "projectname": "《雷州湾》检查测量;《新寮岛至硇洲岛》检查测量;《雷州港》检查测量",
        "projectno": "J2023-011",
        "projecttyp": "规划任务",
        "surveyproj": "高斯-克吕格投影",
        "coordinate": "CGCS2000",
        "surveytype": "检查测量",
        "surend": "2023-07-19 00:00:00",
        "depth": 0
    },
    {
        "guid": "e1203599-84e6-4edc-86e7-e9b786000699",
        "projectname": "《雷州湾》检查测量;《新寮岛至硇洲岛》检查测量;《雷州港》检查测量",
        "projectno": "J2023-011",
        "projecttyp": "规划任务",
        "surveyproj": "高斯-克吕格投影",
        "coordinate": "CGCS2000",
        "surveytype": "检查测量",
        "surend": "2023-07-11 00:00:00",
        "depth": 0
    },
    {
        "guid": "3fc76d8c-996c-4c0a-8a7f-06847ea1e4be",
        "projectname": "《硇洲岛及附近》基本测量",
        "projectno": "G2020-004",
        "projecttyp": "规划任务",
        "surveyproj": "高斯-克吕格投影",
        "coordinate": "CGCS2000",
        "surveytype": "基本测量",
        "surend": "2020-09-03 00:00:00",
        "depth": 0
    }
]

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
计算当天时间差,可以使用Moment.js库中的`moment().startOf('day')`方法。这个方法可以将当前时间设为当天的开始时间,然后再用`moment().diff(moment().startOf('day'), 'milliseconds')`方法计算当前时间和当天开始时间的时间差,单位为毫秒。这样就可以得到当天的时间差了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [moment.js使用](https://blog.csdn.net/qq_53689264/article/details/131207555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [moment.js时间加减处理](https://blog.csdn.net/qq_27674439/article/details/117708915)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [moment.js 常用用法及时区转换](https://blog.csdn.net/weixin_45941959/article/details/122673582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值