javascript - 使用时刻更改日期格式
问题描述
我正在使用具有 moment.js 的日期选择器,目前我得到的日期格式是 MM/DD/YYYY。我想将此格式更改为 DD/MM/YYYY。我怎么做?
这就是我调用日期选择器组件的方式。
<DatePickerComponent
datePickerClass={"form-control quick-checkout-input-filed" + (this.state.errorFields.dob ? " error-field" : "")}
datePickerId="dob"
datePickerName="dob"
datePickerMaxDate={moment(new Date()).format('YYYY-MM-DD')}
datePickerChangeAction={this.onChangeAction}
datePickerBlurAction={this.onBlurAction}
datePickerPlaceholder="Select Date"
/>
这是我的组件。
import React, { PureComponent } from 'react';
// sample DatePickerComponent calling way
class DatePickerComponent extends PureComponent{
constructor(props){
super(props);
this.clickFieldHandler=this.clickFieldHandler.bind(this);
this.onBlurFieldHandler=this.onBlurFieldHandler.bind(this);
this.state={fieldType: "text"};
}
render(){
return(
<div key="name_1">
<input
onFocus={ this.clickFieldHandler }
placeholder={ this.props.datePickerPlaceholder }
type={ this.state.fieldType }
className={ this.props.datePickerClass }
id={ this.props.datePickerId }
name={ this.props.datePickerName }
defaultValue={ this.props.datePickerValue }
min={ this.props.datePickerMinDate }
max={ this.props.datePickerMaxDate }
onChange = { this.props.datePickerChangeAction }
onBlur={ this.onBlurFieldHandler }
disabled={ this.props.datePickerIsDissabled }
/>
</div>
);
}
clickFieldHandler(event){
this.setState({ fieldType: "date" });
}
onBlurFieldHandler(event){
if(event.target.value.length === 0){
this.setState({ fieldType: "input" });
}
if(this.props.datePickerBlurAction){
this.props.datePickerBlurAction(event);
}
}
}
export default React.memo(DatePickerComponent);
解决方案
我目前正在使用时刻库来更改日期格式,例如
Moment('03/03/2021').format('YYYY-MM-DD')
推荐阅读
- javascript - 函数返回未定义的javascript
- node.js - Insomnia 中的“无法读取未定义的属性‘名称’”POST JSON 方法
- c# - 使用 Linq 的大 O 表示法
- r - 使用 tidyverse 自定义圆形条形图
- javascript - Jquery:- 计算每个 tr 中的 td 数
- angular - Angular 8 专注于方法的材料输入
- javascript - 如何调用组件而不调用它(Vue)?
- node.js - 如何在条带中的支付意图示例中自定义支付方式
- sql - 在 PL/pgSQL 函数中返回动态 SQL 语句
- mongodb - 如何使用 Mvc 核心在 mongodb 中插入嵌入式对象