Solve the problem that the acquisition time of antd datepicker is less than 8 hours by default

  • 2021-09-11 19:18:16
  • OfStack

1. Extend the date formatting method


Date.prototype.format = function (fmt) {
 let o = {
  "M+": this.getMonth() + 1,         // Month 
  "d+": this.getDate(),          // Day 
  "h+": this.getHours(),          // Hours 
  "m+": this.getMinutes(),         // Points 
  "s+": this.getSeconds(),         // Seconds 
  "q+": Math.floor((this.getMonth() + 3) / 3), // Quarterly 
  "S": this.getMilliseconds()       // Milliseconds 
 };
 if (/(y+)/.test(fmt)) {
  fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
 }
 for (let k in o) {
  if (new RegExp("(" + k + ")").test(fmt)) {
   fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  }
 }
 return fmt;
};

2. Format the time of the selected date, and the final time is the current time

new Date (value). format ("yyyy-MM-dd hh: mm: ss")

Additional knowledge: antd datepicker sets the start time and deadline to calculate the end time and remove the 6th day of the week

datepicker requires moment formatted time as value, but when operating this value, methods including moment. add () directly change the value of datepicker, even without re-assignment. It needs to be used in conjunction with moment-immutable-methods


import { momentImmutableMethods } from 'moment-immutable-methods'
momentImmutableMethods(moment)

getFinishTime=(value)=>{
    const {getFieldValue} = this.props.form
    let i = 0
    if(typeof(value)==="number"){
      let incomingTime = getFieldValue("incomingTime")
      while(i<value){
        if(incomingTime.addImmu(1,'d').weekday()!==5&&incomingTime.addImmu(1,'d').weekday()!==6){
          i++
          incomingTime = incomingTime.addImmu(1,'d')
        }else{
          incomingTime = incomingTime.addImmu(1,'d')
        }
      }
      this.setState({
        finishTime:value===16?moment():incomingTime,
        disabledFinishTime:value===16?false:true
      })
    }else if(typeof(value)==="object"){
      let deadTime = getFieldValue("deadTime")
      while(i<deadTime){
        if(value.addImmu(1,'d').weekday()!==5&&value.addImmu(1,'d').weekday()!==6){
          i++
          value = value.addImmu(1,'d')
        }else{
          value = value.addImmu(1,'d')
        }
      }
      this.setState({
        finishTime:deadTime===16?moment():value,
        disabledFinishTime:deadTime===16?false:true
      })
    }
 }

Related articles: