1

i have below code

          <DatePicker selected={startDate} name="date_of_sale" id="date_of_sale" onChange={date => setStartDate(date)}  dateFormat="dd/MM/yyyy"/>

when my date is set to startdate const, it has both date and time as 2020-09-10T17:13:12.071Z, but i need only date like 10-09-2020

i have tried dateformat attribute to Datepicker , but it is not changing value format, it only for display..

5
  • Are you retrieving this from a database? And if so would it be possible to change the data datatype to short date? Commented Sep 11, 2020 at 2:28
  • no, it is new value to add Commented Sep 11, 2020 at 2:29
  • Could you use moment and update your state update to date => setStartDate(moment(date).format("dd/MM/yyyy")) ? I know it doesn't solve the issue of the dateFormat prop causing problems. Simply just an alternative to push past it. Commented Sep 11, 2020 at 2:36
  • do i need to include any additional module for moment? Commented Sep 11, 2020 at 2:46
  • i am using react which doesnt have moment js.. it is not supported in higher version Commented Sep 11, 2020 at 3:11

1 Answer 1

1

Are you retrieving this from a database? so you should use Date.parse to parse data to correct format.

//setstartDate is a hook(Usestate) to set startDate.

setstartDate(Date.parse(startDate))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.