Formatting Javascript data

Asked

Viewed 65 times

-2

The date is filled in according to the information obtained from a JSON response

Script

 $(document).ready(function () {
    $("#@(Model.Campo)_Nome").typeahead({
        source: function (query, process) {
            return $.get('@Url.Action("PesquisarAlgumaCoisa", "AlgumaCoisa")', { nome: query, somenteAtivas: '@Model.SomenteAtivas' }, function (data) {
                return process(data);
            }, "json");
        },
        autoSelect: true
    }).change(function (event) {
        var current = $(event.target).typeahead("getActive");
        $("#@Model.Campo").val("");
        $(".dataFim").html("");
        if (current && current.name == $(event.target).val()) {
            $("#@Model.Campo").val(current.id);
            $(".dataFim").html(dataFormatada(current.DataFim));

        }
    });
 function dataFormatada(data){
        dia  = data.getDate().toString(),
        diaF = (dia.length == 1) ? '0'+dia : dia,
        mes  = (data.getMonth()+1).toString(), //+1 pois no getMonth Janeiro começa com zero.
        mesF = (mes.length == 1) ? '0'+mes : mes,
        anoF = data.getFullYear();
    return diaF+"/"+mesF+"/"+anoF;

Perceba que a data vem em formato americano

I’ve tried in some ways, like:

 $(".dataFim").html(current.DataFim.toString("dd/MM/yyyy"));
 $(".dataFim").html(current.DataFim.toLocaleTimeString());

magem

1 answer

0


To solve this case, I converted to string and did the formatting

   $(".dataFim").html(current.DataFim.toString().substr(0, 10).split('-').reverse().join('/'));

Browser other questions tagged

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