React Datepicker( can't get value of input)

前端 未结 7 1677
名媛妹妹
名媛妹妹 2021-02-20 06:25

I am new in react. I need use react-datepicker

I want to get value of input, when I change date. If i click on 20th October 2017, i want put 20th October 2017 in my vari

7条回答
  •  执念已碎
    2021-02-20 07:00

    You can use the getTime() helper function on your date object to get the millisecond timestamp for that specific date, which is a JavaScript number data type. Useful for saving data in the backend of your application. For example Flask Peewee ORM requires a timestamp to be saved for the DateTimeField.

    const [startDate, setStartDate] = useState(new Date())
    
     {
        setStartDate(getTime(date))
      }
    />
    

    source: https://date-fns.org/v2.0.0-alpha.7/docs/getTime

    Combine it with an effect to set the default state value to a timestamp on page load:

    useEffect(() => {
        setStartDate(getTime(startDate))
      }, [])
    
    

    Otherwise for your UI, you can use the format() helper function to get the string value of the given object, with any given format:

    {format(startDate, "MMMM d, yyyy h:mm aa")}

    source: https://date-fns.org/v2.0.0-alpha.7/docs/format

提交回复
热议问题