Reputation: 1
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
Reputation: 1621
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
Upvotes: 0
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
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
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