Reputation: 59
i want to choose date and time with datetime picker react JS
after i choose the date and time i want to get the datetime (2020-08-12 11:08) but the format what i get is (Wed Aug 12 2020 11:08:00 GMT+0700 (Western Indonesia Time))
what should i change in code to change format from (Wed Aug 12 2020 11:08:00 GMT+0700 (Western Indonesia Time)) to (2020-08-12 11:08) and i can view in console.log ? anyone can help me?
this is my code now :
this.state ={
file: null,
getDate: new Date(),
};
this.handleChange = this.handleChange.bind(this);
}
...........
handleChange = date => {
this.setState({
getDate: date
});
};
.............
render() {
console.log(this.state.getDate)
return (
<DateTimePicker
format={"yyyy-MM-dd hh:mm a"}
onChange={this.handleChange}
value={this.state.getDate}
width={200}
/>
);
}
}
Upvotes: 1
Views: 1275
Reputation: 1903
Let's say your date is yourDate
let yourDate = new Date()
console.log("Date you have--> ", yourDate.toString())
//offset to maintain time zone difference
const offset = yourDate.getTimezoneOffset();
yourDate = new Date(yourDate.getTime() + (offset * 60 * 1000));
let modifiedDate = yourDate.toISOString().split('T')[0]+" "+yourDate.toLocaleTimeString()
console.log("Date you want --> ",modifiedDate)
Note Here modifiedDate is a string not a date object
Upvotes: 1
Reputation: 2192
You can. parse it by yourself as mentioned below link
Or use any library like momentjs to parse into whatever format you want.
Upvotes: 2