-1

I am building a web application which requires date to be entered, but on entering a date it will be saved in mm-dd-yyyy and on using bootstrap it will be saved in yyyy-mm-dd format.

Below is the code.

searchTrain(e) {
    e.preventDefault();
    this.props.trains(this.state)
    this.setState({ to: '', from: '', date: '' })
  }

  onChangeTrain(e) {
    this.setState(
      { [e.target.id]: e.target.value }
      //   , () => console.log(this.state)
    );
  }

 <div className="col" >
            <label >
              <span>Journey Date <span className="required">*</span></span>
              <input type="date" className="input-field" id="date"
                value={this.state.date} onChange={this.onChangeTrain} 

                />
            </label>
          </div>
str
  • 42,689
  • 17
  • 109
  • 127
Abhishek Konnur
  • 503
  • 1
  • 9
  • 33

3 Answers3

0

You can try this:

const today = Date.now();

console.log(new Intl.DateTimeFormat('pt-BR', {
  year: 'numeric',
  month: '2-digit',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit'
}).format(today));
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
0

convert both dates before saving, Use momentJS

moment("05-05-2012", 'mm-dd-yyyy').format('dd-mm-yyyy');
Heshan
  • 772
  • 8
  • 22
-1

Using moment will be a plug and play thing include

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/en-ca.js"></script>

var date = moment('30-06-2010', 'DD-MM-YYYY')

date.format('MM-DD-YYYY')
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Vaibhav Chauhan
  • 350
  • 2
  • 8