0

I have a database field with a date as value, it is inserted by an API (which is hosted external) and gives the format yyyy-MM-ddT00:00:00. How can I convert this to a dd-MM-yyyy (no time) format when the value is recalled on the page? EG. 1964-05-11T00:00:00 has to be displayed as 11-05-1964

1

4 Answers 4

1

console.log(convertDate('1964-05-11T00:00:00'));
function convertDate(inputFormat) {
  function pad(s) { return (s < 10) ? '0' + s : s; }
  var d = new Date(inputFormat);
  return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('-');
}

Try this

Sign up to request clarification or add additional context in comments.

Comments

0

If the date formatt of the API is consistent, you could do this:

function formattDate(str) {
   return str.split('T')[0].split('-').reverse().join('-')
}

That's the quickest solution. But if the formatt is not consistent, that is another case.

Comments

0

You could use this package: date-format.

You will be able to do something like that:

var format = require('date-format');
format('dd-MM-yyyy', new Date());

Hope this helps.

Comments

0

Better You can use js library

Source:
https://momentjs.com/
https://momentjs.com/docs/

Example(demo):

http://www.somelesson.blogspot.com/2016/06/momentjs-changing-locales-locally.html

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.