How to convert date at select time and return via Json

Asked

Viewed 178 times

1

In my form I am using the Bootstrap-Datepicker so that the user can inform the desired date at the time of registration, works correctly, but at the time of making a change I am trying to bring the date of the database in the format 99/99/9999 with the command DATE_FORMAT() via MySQL and return the data via Json but the dates are not being converted and not being positioned in the respective fields, my select is like this:

SELECT *, DATE_FORMAT(DataInicial,'%d/%m/%Y') AS DataInicial, DATE_FORMAT(DataFinal,'%d/%m/%Y') AS DataFinal FROM agendaMural WHERE IdAgenda = 2448

But my console.log shows me this result, the dates are not being converted, I tried the alternatives I knew, see how you are:

inserir a descrição da imagem aqui

The page with the return code is this:

if (jQxhr.responseText != "[]") {
    try {
        if (jQxhr.readyState === 4) {
            if (jQxhr.status === 200) {
                var nota = JSON.parse(jQxhr.responseText);
                //Atribui valores aos campos
                $('#idUnidade').val(nota[0].idUnidade);
                $('#IdDepartamento').val(nota[0].IdDepto);  
                $('#dDataInicial').val(nota[0].DataInicial);        
                $('#dDataFinal').val(nota[0].DataFinal);    
                $('#dHoraInicial').val(nota[0].HoraInicial);        
                $('#dHoraFinal').val(nota[0].HoraFinal);    
                $('#sAssunto').val(nota[0].Assunto);
                $('#sLocal').val(nota[0].Local);
                $('#sDescricao').val(nota[0].Descricao);                    
            } else {
                var dialogInstance = BootstrapDialog.show({
                    title: 'ERRO',
                    type: BootstrapDialog.TYPE_DANGER,
                    message: 'Ocorreu um erro na requisição dos dados. Tente novamente.'
                }); 
            }
        }
    }

1 answer

1


You can format your date using jQuery, see an example:

function formatarData(data) {
    var arrData = data.split("-");
    return arrData[2] + "/" + arrData[1] + "/" arrData[0];
}  

var dataInicial = formatarData(nota[0].DataInicial);
var dataFinal = formatarData(nota[0].DataFinal);
$('#dDataInicial').val(dataInicial);
$('#dDataFinal').val(dataFinal);
  • Hello @Pedro Camara Junior, thanks for the tip, the dates were converted correctly, but they are not being assigned to the respective fields, some hint?

  • I made a change in response, created a function to return the formatted date.

  • What do you mean they’re not being assigned?

  • Hello @Pedro Camara Junior, thank you for the function. By doing this $("#dDataInicial"). val(Stardate); the date is not appearing in the field.

  • Display an error in the browser console? Variable name is correct?

  • Yes, variable names are correct and no error is shown on the console, I don’t know where I might be missing

Show 2 more comments

Browser other questions tagged

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