api的應用及antdesign上時間日期插件 選擇時間時固定時間范圍

/* eslint-disable no-unused-vars */
import moment from 'moment'
/**
 *
 * 一下得到的是時間對象
 *
 **/
moment().startOf('day')// 當天的開始00:00:00
moment('2019-12-11')// 2019-12-11 00:00:00
moment('2019-12-11 1:12:12') // 2019-12-11 1:12:12 這一天的此時此分
moment('2019-12-11 1:12:12').startOf('day') // 2019-12-11這一天的凌晨
moment('2019-12-11 1:12:12').add(1, 'days') //  2019-12-12 1:12:12' 即 (2019-12-11 1:12:12的后一天的此刻)
moment('2019-12-11 1:12:12').startOf('day').add(1, 'days')//  2019-12-12 00:00:00  即(2019-12-11 1:12:12的后一天的凌晨)
moment('2019-12-11 1:12:12').endOf('day') // 2019-12-11 23:59:59 即 2019-12-11 這一天的結束時刻
moment('2019-12-11 1:12:12').add(1, 'days').endOf('day') /// / 2019-12-12 23:59:59 即 2019-12-11 這一天的后一天的結束時刻

moment().subtract(7, 'days') // 七天前
moment().subtract(7, 'days').format('YYYY-MM-DD hh:mm:ss') // 得到7天前的年月日時分秒
moment().subtract(7, 'days').format('YYYY') // 得到7天前的年
moment().subtract(7, 'days').format('MM') // 得到7天前的月
moment().subtract(7, 'days').format('DD') // 得到7天前的日
/**
 * 取得格式化時為00:00:00 是有問題的。
 */
// moment().subtract(7, 'days').format('hh') // 得到7天前的年某時的時

moment().subtract(7, 'days').format('mm') // 得到7天前的某時某分的分
moment().subtract(7, 'days').format('ss') // 得到7天前的某時某刻的秒數(shù)

moment().add(1, 'month')// 下一個月的今天
moment().add(1, 'month').format('YYYY-MM-DD hh:mm:ss')
/**
 * 得到指定日期的毫秒數(shù)
 */
// Date.UTC(year, month, day, hours, minutes, seconds, ms)

var hhh = moment('2019-2-1 1:12:12').add(7, 'days').endOf('day')
console.log(hhh._d.getTime())

// Data 時間日期函數(shù)
console.log(Date.UTC(2005, 7, 9))// 得到指定日期 的毫秒數(shù)

/**
 * starttimechange  點擊開始時間的確定按鈕的函數(shù)
 * @param {*} datobj 點擊時間插件后選擇時間,返回的時間對象
 * that.efmobj.endtime 時間插件雙向綁定結束時間
 * this.showtime  值默認情況 默認時間時一天的開始到結束;值 hour:默認時間當前的小時數(shù)到當前小時數(shù)加1
 */

function starttimechange (datobj) {
  let that = this
  let objtime = datobj.clone()
  let startSubend = that.convertmilliseconds(datobj) - that.convertmilliseconds(that.efmobj.endtime)
  // console.log(startSubend, '======開始減去結束時間的毫秒差值')
  switch (this.showtime) {
    case 'hour':
      if ((startSubend > 0) || (startSubend < (-3600000))) that.efmobj.endtime = objtime.add(1, 'hour')
      break
    default:
      switch (true) {
        case startSubend >= 0:
          that.efmobj.endtime = objtime.endOf('day')
          break
        case startSubend < (-604800000):// 7*24*60*60*1000
          that.efmobj.endtime = objtime.add(6, 'day').endOf('day')
          break
      }
  }
}
/**
 * endtimechange  點擊開始時間的確定按鈕的函數(shù)
 * @param {*} datobj 點擊時間插件后選擇時間,返回的時間對象
 * that.efmobj.starttime  時間插件雙向綁定開始時間
 * this.showtime  值默認情況 默認時間時一天的開始到結束;值 hour:默認時間當前的小時數(shù)到當前小時數(shù)加1
 */
function endtimechange (datobj) {
  let that = this
  let objtime = datobj.clone()
  let startSubend = that.convertmilliseconds(datobj) - that.convertmilliseconds(that.efmobj.starttime)
  switch (this.showtime) {
    case 'hour':
      if ((startSubend < 0) || (startSubend > 3600000)) that.efmobj.starttime = objtime.subtract(1, 'hour')
      break
    default:
      switch (true) {
        case startSubend < 0:
          that.efmobj.starttime = objtime.startOf('day')
          break
        case startSubend > 604800000:// 7*24*60*60*1000
          that.efmobj.starttime = objtime.subtract(6, 'day').startOf('day')
          break
      }
  }
}
/**
 * convertmilliseconds 計算時間差
 * @param {*} timestr 時間對象
 * Date.UTC() 轉時間戳的函數(shù)
 */
function convertmilliseconds (timestr) {
  // console.log(timestr.format())
  let datastr = timestr.format()
  let YYYY = datastr.slice(0, 4)
  let MM = datastr.slice(5, 7)
  let DD = datastr.slice(8, 10)
  let hh = datastr.slice(11, 13)
  let mm = datastr.slice(14, 16)
  let ss = datastr.slice(17, 19)
  let millisenconds = Date.UTC(YYYY, MM, DD, hh, mm, ss)
  // console.log(millisenconds, '======', YYYY, MM, DD, hh, mm, ss)
  return millisenconds
}

例子 比較時用時間戳

當點擊開始日期時

  • 當是默認時間的時候 --時間跨度是7天
    起初賦值是當天的開始到當天的結束
    當開始大于結束時,結束=開始天23:59:59
    當開始小于結束且在7天內,默認不變
    當開始小于結束且大于7天 結束=開始+7天
  • 當默認事當前的 時鐘 ---當前時鐘+1 時間跨度1小時
    開始大于結束時,結束=開始+1小時
    開始小于結束且1小時內 結束=默認
    開始小于結束且大于1小時 結束=開始+1小時

當點擊結束日期時

  • 當是默認時間的時候 --時間跨度是7天
    起初賦值是當天的開始到當天的結束
    當結束小于開始時, 開始=結束-1天
    當結束大于開始且在7天內, 開始=默認不變
    當結束大于開始且大于7天 開始=結束-7天
  • 當默認事當前的 時鐘 ---當前時鐘+1 時間跨度1小時
    結束小于開始時, 開始=結束-1小時
    結束大于開始且1小時內 開始=默認
    借宿大于開始且大于1小時 開始=開始-1小時
   <a-col
          class="widthtime"
          :sm="24"
          :md="12"
          :lg="8"
          :lx="8"
          v-if="'starttime' in queryCondition">
          <a-form-item label="開始時間">
            <a-date-picker
              v-model="efmobj.starttime"
              showTime
              :showToday="false"
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="開始時間"
              @ok="starttimechange"
            />
          </a-form-item>
        </a-col>
        <a-col
          class="widthtime"
          :sm="24"
          :md="12"
          :lg="8"
          :lx="8"
          v-if="'endtime' in queryCondition">
          <a-form-item label="結束時間" style="widht:100%;">
            <a-date-picker
              v-model=" efmobj.endtime"
              showTime
              :showToday="false"
              format="YYYY-MM-DD HH:mm:ss"
              @ok="endtimechange"
              placeholder="結束時間"
            />
          </a-form-item>
import moment from 'moment'
//.............
 data () {
    return {
      machineIDList: [],
      comform: {},
      dateFormat: 'YYYY/MM/DD hh:mm:ss',
      efmobj: {
        factoryname: null,
        linename: null,
        processname: null,
        equipmentname: null,
        endtime: moment().endOf('day'),
        starttime: moment().startOf('day')
      },
      factoryList: [],
      lineList: [],
      processList: [],
      equipmentList: [],
      pageobj: {
        pageIndex: 1,
        pageSize: null
      }
    }
  },
 methods: {

    starttimechange (datobj) {
      let that = this
      let objtime = datobj.clone()
      let startSubend = that.convertmilliseconds(datobj) - that.convertmilliseconds(that.efmobj.endtime)
      // console.log(startSubend, '======開始減去結束時間的毫秒差值')
      switch (this.showtime) {
        case 'hour':
          if ((startSubend > 0) || (startSubend < (-3600000))) that.efmobj.endtime = objtime.add(1, 'hour')
          break
        default:
          switch (true) {
            case startSubend >= 0:
              that.efmobj.endtime = objtime.endOf('day')
              break
            case startSubend < (-604800000):// 7*24*60*60*1000
              that.efmobj.endtime = objtime.add(6, 'day').endOf('day')
              break
          }
      }
    },
    endtimechange (datobj) {
      let that = this
      let objtime = datobj.clone()
      let startSubend = that.convertmilliseconds(datobj) - that.convertmilliseconds(that.efmobj.starttime)
      switch (this.showtime) {
        case 'hour':
          if ((startSubend < 0) || (startSubend > 3600000)) that.efmobj.starttime = objtime.subtract(1, 'hour')
          break
        default:
          switch (true) {
            case startSubend < 0:
              that.efmobj.starttime = objtime.startOf('day')
              break
            case startSubend > 604800000:// 7*24*60*60*1000
              that.efmobj.starttime = objtime.subtract(6, 'day').startOf('day')
              break
          }
      }
    },
    convertmilliseconds (timestr) {
      // console.log(timestr.format())
      let datastr = timestr.format()
      let YYYY = datastr.slice(0, 4)
      let MM = datastr.slice(5, 7)
      let DD = datastr.slice(8, 10)
      let hh = datastr.slice(11, 13)
      let mm = datastr.slice(14, 16)
      let ss = datastr.slice(17, 19)
      let millisenconds = Date.UTC(YYYY, MM, DD, hh, mm, ss)
      // console.log(millisenconds, '======', YYYY, MM, DD, hh, mm, ss)
      return millisenconds
    },
 


  
  }
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容