How to change the date format in java script?

2

I am getting the date entered by the user, which selects from a datepicker . In the form the format is displayed right dd/mm/yyyy , but the problem is when I get the date to play in a variável , it comes as follows:

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

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

I will need this information to be inserted in my bank.

    
asked by anonymous 23.03.2016 / 20:33

1 answer

4

Create a function 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)'));

Result:

  

2016/03/23

Fiddle

    
23.03.2016 / 20:38