How to convert date and time with JavaScript? [duplicate]

1

I have a string date in the following format:

  

Dec 11, 2017 01:00AM .

I need to convert the date to the following format:

  

11/12/2017

and the time for this format:

  

01:00 .

I thought of treating the date by means of a conditional and replacing the months as desired, but this will not work for the time as there are many variations. Should there be a function in JavaScript that does this conversion, how can I convert date and time with JavaScript?

    
asked by anonymous 11.12.2017 / 17:51

2 answers

3

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 substr ( ) 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);
  

Once formatted, you can find several posts that will solve your difficulty.

toLocaleString () - Javascript's 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:00 AM   

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:00 PM   

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 do not want to display 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 site 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);
    
11.12.2017 / 18:44
2

Using the Moment.js component is very easy.

Just download or link to the library at: link

And use the code in javascript:

var data = moment("Dec 11, 2017 01:00AM", "lll").format("DD/MM/YYYY HH:mm");

You will write '11 / 12/2017 01:00 'in the variable data .

    
11.12.2017 / 18:12