How to convert date and time with Javascript?

Asked

Viewed 2,644 times

0

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 to treat the date by way of a conditional and replace the months as desired, but this will not work for the time, as there are many variations. There must be a function in Javascript that does this conversion, how can I convert date and time with Javascript?

  • 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/

  • 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.

  • 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 :-\

  • Doing it in the nail is a shot in the foot :-(

  • 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.

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

  • Thank you for the answer! It works perfectly here on the console, but in Zapier’s Code application (Zapier.com/help/code/#requiring-or-using-External-Libraries ) the output is in this format: Mon Dec 11 2017. I believe it is because of the location settings, when using the toLocaleString(), because the company stays in the USA.

  • @Fábiojunioalvessousa, see if the alternative Sem configurações do local in answer answers!

  • Perfect! It worked out fully! Very grateful Leo.

2

Using the Moment.js component is very easy.

Just download or link the library on: https://momentjs.com/

And use javascript code:

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.

  • Thank you for your reply, but I am using the Code application (https://zapier.com/help/code/#requiring-or-using-External-Libraries) of the Zapier online tool, and in this application I cannot link external libraries.

  • You can upload via javascript, I don’t see how the application can block you. :-\

  • The documentation contains the following: "Unfortunately, you cannot require external libraries or install or import libraries commonly referred to as "npm modules". Only the default Node.js library and fetch package are available in the Code application. fetch is already included in the namespace."

  • This date in this format comes from where exactly? who is formatting in this way?

  • It comes from the Google calendar, and the Zapier calendar app that does the formatting.

  • @Zorkind Although it is correct, when a question is marked with the tag [tag:javascript], understand that AP, want a response with pure Javascript, no use of any third party library or API.

  • 1

    I think this tag is too comprehensive to say it’s pure javascript only.

  • Who voted negative, please explain why.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.