If the date is coming in these formats
Dec 11, 2017 01:00AM
or Dec 11, 2017 01:00PM
we have to treat it to a valid format (AM or PM separated by a space)
Dec 11, 2017 01:00 AM
or Dec 11, 2017 01:00 PM
And this can be done using the method replace() that returns the characters in a string starting at the specified location through the specified number of characters:
var recebida = ("Dec 11, 2017 01:00AM");
recebida = recebida.substr(0,18)+" "+recebida.substr(-2);
Done the proper formatting, you can find several posts that will solve your difficulty
toLocaleString() - Javascript native function - date/time string in the format located on your system
var recebida = ("Dec 11, 2017 01:00AM");
recebida = recebida.substr(0,18)+" "+recebida.substr(-2);
var data = new Date(recebida);
var resultado = data.toLocaleString();
console.log (resultado);
Example 1 - Dec 11, 2017 01:00AM
var recebida = ("Dec 11, 2017 01:00AM");
recebida = recebida.substr(0,18)+" "+recebida.substr(-2);
var data = new Date(recebida);
var resultado = data.toLocaleString();
console.log (resultado);
Example 2 - Dec 11, 2017 01:00PM
var recebida = ("Dec 11, 2017 01:00PM");
recebida = recebida.substr(0,18)+" "+recebida.substr(-2);
var data = new Date(recebida);
var resultado = data.toLocaleString();
console.log (resultado);
Example 3 - If you don’t want to show the seconds do
resultado = resultado.substr(0,16);
var recebida = ("Dec 11, 2017 01:00PM");
recebida = recebida.substr(0,18)+" "+recebida.substr(-2);
var data = new Date(recebida);
var resultado = data.toLocaleString();
resultado = resultado.substr(0,16);
console.log (resultado);
No location settings
var recebida = ("Dec 11, 2017 01:00AM");
recebida = recebida.substr(0,18)+" "+recebida.substr(-2);
var data = new Date(recebida);
var dia = data.getDate();
if (dia.toString().length == 1)
dia = "0"+dia;
var mes = data.getMonth()+1;
if (mes.toString().length == 1)
mes = "0"+mes;
var ano = data.getFullYear();
var hora = data.getHours();
if (hora.toString().length == 1)
hora = "0"+hora;
var minutos = data.getMinutes();
if (minutos.toString().length == 1)
minutos = "0"+minutos;
console.log (dia+"/"+mes+"/"+ano+ " "+hora+":"+minutos);
Or briefly
var recebida = ("Dec 11, 2017 01:00PM");
recebida = recebida.substr(0,18)+" "+recebida.substr(-2);
var data = new Date(recebida);
var dataFormatada = ("0" + data.getDate()).substr(-2) + "/" + ("0" + (data.getMonth() + 1)).substr(-2) + "/" + data.getFullYear() +" " + ("0"+ data.getHours()).substr(-2)+":"+("0"+ data.getMinutes()).substr(-2);
console.log(dataFormatada);
For date and time format manipulations I recommend using the Moment js component that already does safely and without chance of miscalculation etc. https://momentjs.com/
– Zorkind
with the Moment just call this way: Moment("Dec 11, 2017 01:00AM", "lll"). format("L") and then add the time or use a custom format.
– Zorkind
The issue here is that as it is coming in English format "Dec" the Date.parse fails because it will be configured for Brazil, then you will have to make even a code in hand, building a date in the hand based on the month abbreviations in English :-\
– Zorkind
Doing it in the nail is a shot in the foot :-(
– Zorkind
The main focus of the difficulty in the question, I think, would be in the format of the date received which is not a valid format, so I don’t think it is
possível duplicata
, because, this punctual problem is not being addressed in other posts.– user60252