VUE 之 Element 限定日期范围pickerOptions使用方法及绕坑指南

pickerOptions: {
   disabledDate: time => {
          let timeTmp = time.getTime();
          let startTmp = startDate.getTime()  - 24 * 60 * 60 * 1000;
          let endTmp = endDate.getTime();
	      return timeTmp < startTmp || timeTmp > endTmp;
     }
}

问: 为什么加多一天时间 (24 * 60 * 60 * 1000为一天)?
答: 因为element组件选取日期返回的时间是当天的0点;

以下绕坑
举个使用日期栗子(和传入element限定日期为什么要减一天原理一致):
当你选中element日期是2019年12月10号,
element返回的数据是 Tue Dec 10 2019 00:00:00 GMT+0800 (中国标准时间)
element返回的是这一天的 00:00:00秒,
转换时间戳是 1575907200000.
假设后台给我的数据是10号的中午12点,时间戳是1575950400000.
则判断错误的方法是:
1575950400000 < 1575907200000
而正确方法是:
1575950400000 < 1575907200000+(24 * 60 * 60 * 1000 -1)
这里细品, 不理解请动手转换时间戳就明白了;

时间戳转换方法如下:
new Date(1575907200000)
Tue Dec 10 2019 00:00:00 GMT+0800 (中国标准时间)

示例:https://www.cnblogs.com/cengjingdeshuige/p/11169629.html

发布了127 篇原创文章 · 获赞 150 · 访问量 48万+

猜你喜欢

转载自blog.csdn.net/qq_40259641/article/details/103976627