1
I have a modal filled via Javascript. The Javascript code is as follows::
// INTERFACE MODAL viewVenda
$(document).ready(function () {
$(document).on('click', '.view_data', function () {
var id = $(this).attr("id");
if (id !== '') {
var dados = {
id: id
};
$.post('dsVenda.php', dados, function (retorna) {
var venda = ['', '', '', '', '', '', '', '', '', ''];
venda = retorna.split(',');
$("#var0").html(venda[0]);
$("#var1").html(venda[1]);
$("#var2").html(venda[2]);
$("#var3").html(venda[3]);
$("#var4").html(venda[4]);
$("#var5").html(venda[5]);
var data = venda[6];
data_formatada = data.toLocaleDateString('pt-br');
$("#var6").html(data_formatada);
$("#var7").html(venda[7]);
$("#var8").html(venda[8]);
$("#var9").html(venda[9]);
$('#viewVenda').modal('show');
});
}
});
});
The variable contained in sale[6] has the value 2020-03-06 15:11:15 registered in the database.
I am formatting via native Javascript function for the Brazil dd/MM/yyyy standard but the modal does not open. If I show the gross value as follows it opens.
$("#var6").html(venda[6]);
The same problem happens if I try to format sale[3] and sale[5] value which is monetary
Just a detail, if you want to always force the Brazilian format, pass the locale as a parameter:
data.toLocaleDateString('pt-BR')
- otherwise it will take the locale that is configured in the browser, and it is not always guaranteed that it ispt-BR
- ex: https://repl.it/repls/SandyOvercookedAlgorithm - Anyway, where is the modal? It gives an error (and if so, which is the message)?venda[6]
is a string in the format "2020-03-06 15:11:15" or is aDate
? (you can’t test that code without knowing that information, and maybe is why the question is receiving negative votes)– hkotsubo
The mistake is that the
toLocaleDateString
only works with objectDate
. Because of the error the script stops running.– Sam
sale[] is a vector. picked the position [6] of it.
– Flávio Kowalske
Yes, but position 6 has a string, a
Date
, or what? If he doesn’t have oneDate
, then the problem is what Sam said above. If he has aDate
, then the problem must be elsewhere, etc.. Do you understand how there’s no way we can test and only with the information of the question is the only option is to guess? That could explain the negative votes... Please click [Edit] and enter the necessary information so that anyone can test (I suggest you read how to assemble a [mcve] <<< here are several tips for setting a suitable example)– hkotsubo
var data = venda[6];
is a string, you are trying to use the methodtoLocaleDateString
which is the Date object, as @Sam said. You can use"2020-03-06 15:11:15".replace(/(\d{4})\-(\d{2})\-(\d{2})(.*)/, "$3/$2/$1");
in place.– Benilson
@Benilson worked out with the date field. I tried to make a REGEX to use with monetary fields but it did not work, as it would be to format type R $ 32.000,00?
– Flávio Kowalske