Send date format javascript via ajax

0

I'm trying to send data from a registration form via Ajax / javascript to a java service, however the database receives the data in date format and I do not know what to do to convert to date, how do I perform this conversion? / p>

function consumir() {

  $.ajax({

    type: "POST",
    url: "http://dalvz/core-web/rest/usuarios",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: true,
    cache: true,
    data: JSON.stringify({
      "nome": document.getElementById('nome').value,
      "sobrenome": document.getElementById('sobrenome').value,
      "dataNascimento": document.getElementById('dataNascimento').value,
      "email": document.getElementById('email').value,
      "senha": document.getElementById('senha').value
    }),

    success: function (data) {
      alert(data.nome);
    }

  });
}
    
asked by anonymous 09.06.2017 / 01:36

1 answer

0

Assuming you are using the date in the form "d / m / A", to insert into the Mysql database it should be in "Y / m / d".

Your code should look like this:

function consumir() {

    var data = document.getElementById('dataNascimento').value.split("-");
    var dataNascimento = new Date(data[2], data[1], data[0]);

    $.ajax({

        type: "POST",
        url: "http://dalvz/core-web/rest/usuarios",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: true,
        data: JSON.stringify({
            "nome": document.getElementById('nome').value,
            "sobrenome": document.getElementById('sobrenome').value,
            "dataNascimento": dataNascimento,
            "email": document.getElementById('email').value,
            "senha": document.getElementById('senha').value
        }),

        success: function(data) {
            alert(data.nome);
        }

    });
}
    
09.06.2017 / 01:48