user13107258
user13107258

Reputation: 59

how to get exact time what choosen on datetime picker react JS

i want to choose date and time with datetime picker react JS

image datetime picker in my web

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

Answers (2)

kapil pandey
kapil pandey

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

pritesh
pritesh

Reputation: 2192

You can. parse it by yourself as mentioned below link

Convert date from 'Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)' to 'YYYY-MM-DD' in javascript

Or use any library like momentjs to parse into whatever format you want.

Upvotes: 2

Related Questions