To my bad luck, an API that we use in a project, returns the dates with the string format 211119
, taking into account that this date is 2021/11/19
I would like to be able to create an object Date
and be able to compare it with another... obviously the first thing I try is to apply a slice
, create the date but I believe the date 1921/11/19
.
var fecha = '211119';
var anio = fecha.slice(0, 2);
var mes = parseInt(fecha.slice(2, 4));
var dia = fecha.slice(4, 6);
console.log(moment(new Date(anio, mes - 1 , dia)).format('DD/MM/YYYY'))
<script src="https://momentjs.com/downloads/moment.js"></script>
Output:
1921/11/19
In Javascript, the two digits default to dates between 1900-1999, as you can see here .
But with
moment.js
you can correctly create your objectDate
by applying the methodtoDate
as follows:Output: