判断指定日期是不是n天后

语法

  1. import { isTomorrow } from 'warbler-js'
  2. const result = isTomorrow(date, n)

参数

  • date (String) : 指定日期,可传参数同 new Date(),并且支持 yyyy-mm-dd格式 ,不传默认获取当天。
  • n (Number) : n 天后,不传默认为 1 ,也就是明天。

返回值

Booleantruen 天后, false 不是 n 天后。

源码

  1. const isTomorrow = (date, n = 1) => {
  2. const curDate = new Date(); // 当前日期
  3. curDate.setDate(curDate.getDate() + n); // 当前日期加一天
  4. // 指定日期
  5. const tarData = date ? new Date(typeof date === 'string' && date.includes('-') ? date.replace(/-/g, '/') : date) : new Date();
  6. return ['getFullYear', 'getMonth', 'getDate'].every((i) => curDate[i]() === tarData[i]());
  7. };

例子

  1. import { isTomorrow } from 'warbler-js'
  2. // 测试日期为2021-09-26
  3. const result1 = isTomorrow(new Date())
  4. const result2 = isTomorrow("2021-09-27",1)
  5. const result3 = isTomorrow("2021-09-27",2)
  6. const result4 = isTomorrow("2021-09-28",2)
  7. console.log(result1) //=> false
  8. console.log(result2) //=> true
  9. console.log(result3) //=> false
  10. console.log(result4) //=> true