简体   繁体   中英

Changing date format using moment

I'm using a date picker which has moment.js and currently the date format I'm getting is MM/DD/YYYY. I want to change this format to DD/MM/YYYY. How do I do that?

This is how I call the date picker component.

<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"
/>

I've also attached a screenshot of the datepicker. 日期选择器

This is my component.

 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);

I m currently using moment library to change date formate like

Moment('03/03/2021').format('YYYY-MM-DD')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM