javascript – Date in odd fractional format 0.00xxxxxxx


I get this date from the BD:


I would like to switch to this format: dd/mm/yyyy , using pure javascript or jquery .

How I do?

Here is the ASP that assembles the select:

strsql = ""
                strsql = "select a.cod_operadora, a.nom_operadora, to_char(b.dat_exclusao,'dd/mm/yyyy') dat_exclusao, case when b.dat_exclusao is null then 'N' else 'S' end excluido "
                strsql = strsql & " from ts_odo.odo_operadora             a "
                strsql = strsql & "     ,ts_odo.odo_prestador_operadora   b "
                strsql = strsql & "where a.cod_operadora    = b.cod_operadora "
                strsql = strsql & "  and b.cod_prestador_ts = " & cod_prestador_ts
                strsql = strsql & "order by to_number(a.cod_operadora)"                     

                set TopDB = server.CreateObject("TSDB.Data")

                set rsOperadora = TopDB.objrs ( CStr(txt_usuario), _
                                                CStr(txt_senha), _
                                                CStr(txt_ip), _
                                                session("ace_sistema"), _
                                                CStr(txt_modulo), _
                set TopDB = nothing 

And here is the call to the js method, which is inside a while in asp:

Response.Write "<script>montaDataSubstituicaoPrestador(" & rsOperadora("dat_exclusao") & ")</script>"

And that's the js function that should do what I want, print a label on the calculated date.

function montaDataSubstituicaoPrestador(dt_exclusao){

    alert('Paulo: ' + dt_exclusao);

    var arrData = dt_exclusao.split('/');
    var exclusaoFormatada = arrData[1] + '-' + arrData[0] + '-' +  arrData[2];
    var dias = parseInt(prazoSubPrestador);
    var novaData = new Date(arrData[2], arrData[1] - 1, arrData[0]);

    novaData.setDate(novaData.getDate() + dias);

    hoje = new Date(novaData)
    dia = hoje.getDate()
    mes = hoje.getMonth()
    ano = hoje.getFullYear()
    if (dia < 10)
        dia = "0" + dia

    if((mes+1) < 10)
        mes = "0" + (mes+1);

    if (ano < 2000)
        ano = "19" + ano

    var dt = dia + "/" + (mes) + "/"+ano;

    var elem = document.getElementById('ind_exclusao_voluntaria');

    if(elem.value == 'S')
        document.getElementById('lblPrazoSubPrestador').innerHTML = "Prazo de substituição: " + dt;
        document.getElementById('lblPrazoSubPrestador').innerHTML = "";


The problem may be in this excerpt

Response.Write "<script>montaDataSubstituicaoPrestador(" & rsOperadora("dat_exclusao") & ")</script>"

You are passing the parameter without quotes. As the value is numeric and contains the mathematical division character / , a mathematical operation is taking place when the parameter is informed in the invocation of the rsOperadora() function.

To solve this, enter the parameter enclosed in quotes so that it is treated as a string.


Response.Write "<script>montaDataSubstituicaoPrestador('" & rsOperadora("dat_exclusao") & "')</script>"

I just delimited it with single quote. This ensures that the value is read as a string.

obs: I'm not sure this is exactly what happens because the information in the question is confusing and vague.

Explanation of how I arrived at this deduction

I deducted due to the format of the value 0.002976190476190476 . The first thing I figured out is, how did you manage to get a date into this format?

By logical deduction I imagined 20/01/2016 (today's date as an example). There are 20 divided by 1 which is divided by 2016, equal to 0.0099206349206349 . Ready! We have the strange format identical to the one posted.

What makes it confusing is that in your question you "claim" that the value already comes from the database. But looking at the query query, it didn't make sense. However, when looking at this snippet of code & rsOperadora("dat_exclusao") & , it started to make sense where the problem might be.


To be sure about what I've deduced, just run the ASP page normally and read the HTML code generated in the browser. In Chrome, press CTRL+U which will open a window showing the generated HTML code. Look for the snippet <script>montaDataSubstituicaoPrestador( . You'll probably see that the date is correct, something like <script>montaDataSubstituicaoPrestador(20/01/2016)</script> , but without the quotes. That way, without the quotes, it's a mathematical operation taking place, resulting in the fractional value.

Scroll to Top