How to change the date format in java script?

Asked

Viewed 1,332 times

2

I’m taking the date informed by the user, who selects from a datepicker. In the form the format is displayed right dd/mm/yyyy, but the problem is when I take the date to play in a variável, she comes in the following way:

Wed Mar 23 2016 00:00:00 GMT-0300 (BRT)

How do I convert variável in yyyy/mm/dd?

'cause I’m gonna need that information to get it into my bank.

1 answer

4


Create a role for this:

function formatarData(data) {
    var d = new Date(data),
        mes = '' + (d.getMonth() + 1),
        dia = '' + d.getDate(),
        ano = d.getFullYear();

    if (mes.length < 2) mes = '0' + mes;
    if (dia.length < 2) dia = '0' + dia;

    return [ano, mes, dia].join('/');
}


alert(formatarData('Wed Mar 23 2016 00:00:00 GMT-0300 (BRT)'));

Upshot:

2016/03/23

Fiddle

  • Thanks Marllon, I’ll try.

  • There are other ways, okay? If you want me to edit the answer and add it to you... but this solution, in my opinion, is the simplest.

Browser other questions tagged

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