Search code examples
javascriptdate-conversion

Converting date format javascript database


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


Solution

  • 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.