Convert bank date to AJAX

Asked

Viewed 270 times

0

I would like to convert the bank coming date (2012-02-10) to Brazilian standard (10/02/2012) in AJAX Success, example:

 $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "crud/consulta.php",
        data: dados,
        success: function(data) {
            $('.dataFiltro').val(data.data);
            // AQUI ALGUMA CONVERSÃO
});

Or some way to do the conversion in the form input, being that, this value will be filled in an input. Some suggestion w

  • Blow her up with the split and change the order, like this Jsfiddle

  • 1

    Thank you William.

  • I recommend using regular expression. data.data.replace(/^(\d+)\-(\d+)\-(\d+)$/, "$3/$2/$1").

  • How would you do with a date and time field?

  • When you say "date of the bank’s arrival (2012-02-10)" Is that always the format? How are you setting the time on the server?

  • You saw my question here? ^

  • For example, the format always comes (2012-02-10 12:25:50) accurate convert to 10/02/2012 12:25:50

Show 2 more comments

2 answers

2


In the case of a date and time field:

<input type="datetime-local" class="dataFiltro" />

You don’t need to convert, just concatenate the date with the time:

...
success: function(data) {
    var novaData = data.data;
    novaData = novaData + "T15:35"; //exibirá no formato: 07/07/2016 15:35
    $('.dataFiltro').val(novaData);
}
...
  • How would you do with a date and time field?

  • I edited what I had posted to display in the date and time field.

0

You can convert the date in the query and already bring it in the correct format.

  • In the query or service that takes query values and writes to JSON. In general it is always good to define the correct format already on the server, even if a field is used datetime onscreen.

Browser other questions tagged

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