0

I'm getting my backend date as follows:

2020-01-01T03:00:00.000Z

I am using the daterangepicker from the AdminLTE template.

my field:

<div className="col-sm-3">
  <label>Contratação *</label>
  <Field
    name="dt_contracting"
    type="date"
    className="form-control"
  />
</div>

I want to convert the received date, to the format "DD/MM/YYYY"

input in insert

my input is already in the correct format to select, but when I receive the Back-End date, it is not filled due to difference in format.

input in editing

1

2 Answers 2

2

Easiest way is to use the built-in toLocaleDateString() function. MDN

In your case you can use it like this:

function toPrettyDate( rawDateStr ) {
    const date = new Date( rawDateStr );
    return date.toLocaleDateString('en-GB'); // Should really use user-based locale here
}
Sign up to request clarification or add additional context in comments.

5 Comments

The accepted answer on your linked "duplicate" is not the best answer for this user's use case. It is overly complex.
Ah yes, Stack Overflow, the site I come to when I want to read through 56 answers and decide on the best one when I didn't know the answer in the first place. How user friendly.
Sure, you'd rather them go through thousands of duplicate questions, each with slightly different answer, and having the same person try to figure out which one is better. Yeah, no, much more user friendly.
UTC string => DD/MM/YYYY is a different question than Date instance => DD-mon-YYYY. You or I may understand the inherent similarities in the question and know how to convert one problem into another, but someone asking this type of question may not even know how to go from UTC string => Date.
If you spent half the time you spent on these comments actually looking for similar questions, you'd find there are hundreds of questions about dates, strings containing dates, etc. on Stack Overflow. For instance, Convert Date from one format to another format in JavaScript
0

You can use momentjs.

Prefer this for installation https://www.npmjs.com/package/react-moment

https://momentjs.com/docs/ for examples

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.