0
state = {trip: date: ""}; 

changevalueDate = e => {
 let trip = this.state.trip; 
 trip.date = e.target.value; 
 this.setState({ trip });
};

<TextField label="Start Date" name="Date" 
 InputLabelProps={{ shrink: true, required: true }} 
 type="date" onChange={e => this.changevalueDate(e)}
/>
akhtarvahid
  • 9,445
  • 2
  • 26
  • 29

3 Answers3

1

Here is the solution:

const date = new Date("2019-12-08T04:23:53.949Z");
//in your case: new Date(state.date)

const mm = date.getMonth() + 1;
const dd = date.getDate();
const yy = date.getFullYear();

const newDate = yy + "-" + mm + "-" + dd;
console.log(newDate);

method 2:

const date2 = "2019-12-08T04:23:53.949Z"
const newDate2 = date2.substring(0, date2.indexOf("T"));
console.log({ newDate2 });
Sushilzzz
  • 527
  • 5
  • 20
1

if you want to use moment. install that

-step 1

npm i moment

-step 2

const str = '2019-12-08T04:23:53.949Z'
console.log(moment(str).format('MM/DD/YYYY'))
akhtarvahid
  • 9,445
  • 2
  • 26
  • 29
1

Duplicate of this

Your initial string is an ISO 8601 date. If all you care about is the YYYY-MM-DD part, you can simply take the 10 first strings of your date:

const dateStr = '2019-12-08T04:23:53.949Z'
const newDate = dateStr.substring(0,10)
Anas Tiour
  • 1,344
  • 3
  • 17
  • 33