antd datepicker 如何调整时间偏差,使其显示正确的时间而非少8小时?

更新于
2026-07-26 10:42:20
21阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计472个文字,预计阅读时间需要2分钟。

antd datepicker 如何调整时间偏差,使其显示正确的时间而非少8小时?

javascriptDate.prototype.format=function(fmt) { let o={ M+: this.getMonth() + 1, // 月份 d+: this.getDate(), // 日 h+: this.getHours(), // 时 m+: this.getMinutes(), // 分 s+: this.getSeconds(), // 秒 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;};

1、扩展日期格式化方法

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、对选中日期的时间进行格式化处理,最终得到的时间就是当前时间

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

补充知识:antd datepicker设置开始时间和期限计算出结束时间并且去除周六日

antd datepicker 如何调整时间偏差,使其显示正确的时间而非少8小时?

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 获取时间默认少8个小时的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计472个文字,预计阅读时间需要2分钟。

antd datepicker 如何调整时间偏差,使其显示正确的时间而非少8小时?

javascriptDate.prototype.format=function(fmt) { let o={ M+: this.getMonth() + 1, // 月份 d+: this.getDate(), // 日 h+: this.getHours(), // 时 m+: this.getMinutes(), // 分 s+: this.getSeconds(), // 秒 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;};

1、扩展日期格式化方法

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、对选中日期的时间进行格式化处理,最终得到的时间就是当前时间

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

补充知识:antd datepicker设置开始时间和期限计算出结束时间并且去除周六日

antd datepicker 如何调整时间偏差,使其显示正确的时间而非少8小时?

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 获取时间默认少8个小时的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。