如何解决 RangeError: Invalid time value [英] How to resolve RangeError: Invalid time value
问题描述
利用 React,我使用 moment 和 react-datepicker 库设置了一个 DatePicker 组件,并将使用今天的日期以 MM-DD-YYYY 格式计算的初始日期值传递给它.
Making use of React, I have set up a DatePicker component using moment and react-datepicker library and am passing it an initial date value calculated using today's date in the MM-DD-YYYY format.
当我尝试加载时出现此错误行:
When I try to load I get this error line :
throw new RangeError('时间值无效');将系统时区中的日期转换为 UTC+00:00 时区中的相同日期.这样可以确保在实现 UTC 功能时,语言环境将与其兼容.`
throw new RangeError('Invalid time value'); Convert the date in system timezone to the same date in UTC+00:00 timezone.This ensures that when UTC functions will be implemented, locales will be compatible with them.`
DatePicker 组件:
DatePicker component:
class DatePicker extends Component {
render() {
return (
// <input type="date"
// className={this.props.className + " myInput"}
// placeholder={this.props.placeholder}
// onChange={this.props.onChange}
// value={this.props.value}
// />
<DatePicker2
dateFormat="DD MMM YYYY"
className={this.props.className + " myInput"}
selected={moment(this.props.value)}
onChange={this.props.onChange}
readOnly={true}
/>
);
}
}
export default DatePicker;
我的问题是:如何将系统时区中的日期转换为 UTC+00:00 时区中的相同日期(假设这是解决方案,但如果不是,我该如何解决?
My question is: how do I Convert the date in system timezone to the same date in UTC+00:00 timezone (assuming that's the solution, but if not, how do I resolve this?
推荐答案
要重新格式化您可以使用的日期:
To reformat the date you can use:
moment(this.props.value).format("YYYY-MM-DD HH:mm Z");
这篇关于如何解决 RangeError: Invalid time value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!