antd RangePicker限制选择时间跨度是30天

const { RangePicker } = DatePicker;
 const [selectDate, setSelectDate] = useState(null);
 /* 控制下单时间选择范围30天 */
   const disabledTaskDate = (current) => {
    if (!current || !selectDate) return false;
    const offsetV = 2592000000;                 //30天转换成ms
    const selectV = selectDate.valueOf();
    const currenV = current.valueOf();
    return (calcMinus(currenV, offsetV) > selectV || calcAdd(currenV, offsetV) < selectV) ? true : false;
  }

  /* 选择任务时间变化 */
  const onDateChange = (dates) => {
    if (!dates || !dates.length) return;
    setSelectDate(dates[0]);
  }

  const onDateOpenChange = () => {
    setSelectDate(null);
  }

下面是以上涉及到的浮点数相加减的定义

/* 两个浮点数相减 */
export const calcMinus = (num1, num2) => {
  num1 = Number(num1);
  num2 = Number(num2);
  if (isNaN(num1) || isNaN(num2)) return 0;

  const num1Digits = (num1.toString().split(".")[1] || "").length;
  const num2Digits = (num2.toString().split(".")[1] || "").length;
  const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits));
  return (calcMulti(num1, baseNum) - calcMulti(num2, baseNum)) / baseNum;
};```

export const calcAdd = (num1, num2) => {
  num1 = Number(num1);
  num2 = Number(num2);
  if (isNaN(num1) || isNaN(num2)) return 0;

  const num1Digits = (num1.toString().split(".")[1] || "").length;
  const num2Digits = (num2.toString().split(".")[1] || "").length;
  const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits));
  return (calcMulti(num1, baseNum) + calcMulti(num2, baseNum)) / baseNum;
}

```go
/* 两个浮点数相乘 */
export const calcMulti = (num1, num2) => {
  num1 = Number(num1);
  num2 = Number(num2);
  if (isNaN(num1) || isNaN(num2)) return 0;

  const num1String = num1.toString();
  const num2String = num2.toString();
  const num1Digits = (num1String.split(".")[1] || "").length;
  const num2Digits = (num2String.split(".")[1] || "").length;
  const baseNum = Math.pow(10, num1Digits + num2Digits);
  return (
    (Number(num1String.replace(".", "")) *
      Number(num2String.replace(".", ""))) /
    baseNum
  );
};

你可能感兴趣的:(javascript,es6,前端)