簡體   English   中英

使用時刻更改日期格式

[英]Changing date format using moment

我正在使用具有 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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM