微信小程序 引入DayJS 用于时间处理

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

详情参考文档:https://dayjs.fenxianglu.cn/category/manipulate.html#%E5%8A%A0%E4%B8%8A

第一步:

 npm install dayjs --save

第二步,点击小程序工具,Npm构造器

 JS 中引入

var dayjs = require('dayjs')

 然后,我们就可以用DayJS 了 

      let bol=dayjs().isBefore(dayjs('2021-12-15'));

当前时间是否在 2021年12月15日之前

我在组件的【onLoad】方法中测试了下

  lifetimes: {
 
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.GetData();
      var s = dayjs().format();
      console.log(s);
    }
  },

 获取当前时间 并 格式化

      var endTime = dayjs().format("YYYY-MM-DD");
      
      var startTime = dayjs(endTime).subtract(6, 'day').format("YYYY-MM-DD");
endTime :当前时间 格式为 YYYY-MM-dd
startTime :当前时间向前推6天

 @天才卧龙的博客

原文地址:https://www.cnblogs.com/chenwolong/p/15594635.html