zoukankan      html  css  js  c++  java
  • moment笔记

    moment计算时间

    1、moment加法

    console.log(
     moment(new Date()).add(1, 'days').format('YYYY-MM-DD')
    ) // 2019-10-26 ; 在今天的基础上加了一天
    console.log(
     moment(new Date()).add(1, 'months').format('YYYY-MM-DD')
    ) //2019-11-25 ; 在本月的基础上加了一天
    console.log(
     moment(new Date()).add(1, 'years').format('YYYY-MM-DD')
    ) // 2020-10-25 ; 在今年的基础上加了一天

    2、moment减法

    console.log(
     moment(new Date()).subtract(1, 'days').format('YYYY-MM-DD')
    ) // 2019-10-24 ; 在今天的基础上减了一天
    console.log(
     moment(new Date()).subtract(1, 'months').format('YYYY-MM-DD')
    ) //2019-09-25 ; 在本月的基础上减了一天
    console.log(
     moment(new Date()).subtract(1, 'years').format('YYYY-MM-DD')
    )

    3、moment计算两个时间点的时间差

    const start = moment(moment(self.props.form.getFieldValue('rentTime')).format('YYYY-MM-DD'))
    const end = moment(moment(dateString).add(1, 'days').format('YYYY-MM-DD'))
    self.props.form.setFieldsValue({
     rentLife: end.diff(start, 'day')
    }); // self.props.form.getFieldValue 和 self.props.form.setFieldsValue 是 antd form 的 api

    3、moment计算两个时间点先后顺序

    const currentDate = new Date()
    const start = moment(self.props.form.getFieldValue('rentTime')).format('YYYY-MM-DD')
    return currentDate.isBefore(moment(start).add(0, 'days'))
                            
    
    const currentDate = new Date()
    const start = moment(self.props.form.getFieldValue('rentTime')).format('YYYY-MM-DD')
    return currentDate.isAfter(moment(start).add(0, 'days'))

  • 相关阅读:
    Intelij根据数据库表生成实体类
    RabbitMQ(二):入门案例
    RabbitMQ(一):简介和基本概念
    Python 第十课,面向对象补足,异常处理
    Python 第九课,面向对象进阶
    Python 第七课,模块
    Python 第六课,装饰器,生成器,迭代器,其他
    Python 第五课,正则表达式
    Python 第四课,内置函数,I/O操作。
    Python 第三课,函数。
  • 原文地址:https://www.cnblogs.com/lan1974/p/11868139.html
Copyright © 2011-2022 走看看