Convert a date to another format
How do I convert a date in YYYY-MM-DD[T]HH:mm:ss.SSS[Z]
format (for example 2017-09-21T21:00:00.000Z
) to DD.MM.YYYY
format (for example 11.05.2017
)?
You can use moment
.
1
3 answers
I advise you to use a small and convenient or dateformat In this case, the code will look like:
var d = new Date('2017-09-21T21:00:00.000Z');
dateformat(d, 'DD.MM.YYYY')
Here is the link to the doc: https://www.npmjs.com/package/dateformat
1
Author: CoonJS, 2017-09-21 11:14:30
For example:
var a = new Date('2017-09-21T21:00:00.000Z');
var res = [
addLeadZero(a.getDate()),
addLeadZero(a.getMonth() + 1),
a.getFullYear()
].join('.');
console.log(res);
function addLeadZero(val) {
if (+val < 10) return '0' + val;
return val;
};
1
Author: Rostyslav Kuzmovych, 2017-09-20 16:57:34
You can use .toLocaleString()
:
var options = {
day: 'numeric',
month: 'numeric',
year: 'numeric'
}
function getDate(str) {
var date = new Date(str);
return date.toLocaleString('ru', options)
}
console.log(getDate('2017-09-21T21:00:00.000Z'));
1
Author: Cheg, 2017-09-20 17:14:47