0

I receive a date from server like 2017-08-12T00:00:00+00:00. I need to display it in dd/mm/yyyy format.

How should I manipulate it? Is moment required or is it simple string processing?

1 Answer 1

1
let getFormat = (dateString) => {
   let date = new Date(dateString);
   return date.getDate()+"/"+(date.getMonth() + 1)+"/"+date.getFullYear();
}
getFormat("2017-08-12T00:00:00+00:00");

If you want to prepend 0 then use:

let getFormat = (dateString) => {
   let date = new Date(dateString);
   let day = date.getDate() < 10 ? "0"+date.getDate() : date.getDate();
   let month = (date.getMonth() + 1) < 10 ? "0"+(date.getMonth() + 1) : (date.getMonth() + 1);

   return day+"/"+month+"/"+date.getFullYear();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Be aware that this will not add 0 in front of single digit days/months, so you can get something like d/m/yyyy
Okay let me try

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.