解決antd datepicker 獲取時間默認(rèn)少8個小時的問題
1、擴(kuò)展日期格式化方法
Date.prototype.format = function (fmt) { let o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小時 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; 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、對選中日期的時間進(jìn)行格式化處理,最終得到的時間就是當(dāng)前時間
new Date(value).format(“yyyy-MM-dd hh:mm:ss”)
補(bǔ)充知識:antd datepicker設(shè)置開始時間和期限計(jì)算出結(jié)束時間并且去除周六日
datepicker 需要使用moment格式的時間作為value,但是在操作 此value時,包括moment.add()等方法都會直接改變datepicker的值,即使沒有重新賦值。此時需要配合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 }) } }
以上這篇解決antd datepicker 獲取時間默認(rèn)少8個小時的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue實(shí)現(xiàn)Excel預(yù)覽功能使用場景示例詳解
這篇文章主要為大家介紹了Vue實(shí)現(xiàn)Excel預(yù)覽功能使用場景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09vue項(xiàng)目之前端CryptoJS加密、解密代碼示例
在Vue項(xiàng)目中集成CryptoJS進(jìn)行數(shù)據(jù)加密,首先需要通過npm安裝CryptoJS安裝包,然后在項(xiàng)目文件中引入CryptoJS,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11vue 實(shí)現(xiàn)通過vuex 存儲值 在不同界面使用
今天小編就為大家分享一篇vue 實(shí)現(xiàn)通過vuex 存儲值 在不同界面使用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue3使用富文本框(wangeditor)的方法總結(jié)
項(xiàng)目中用到了富文本,選來選去選擇了wangeditor,下面這篇文章主要給大家介紹了關(guān)于Vue3使用富文本框(wangeditor)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01