geenidee
geenidee

Reputation: 1

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

Upvotes: 0

Views: 811

Answers (4)

Chang
Chang

Reputation: 1716

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.

Upvotes: 0

Durga
Durga

Reputation: 15604


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

Upvotes: 1

Adrien Blanquer
Adrien Blanquer

Reputation: 2061

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.

Upvotes: 0

Related Questions