Date in odd fractional format 0.00xxxxxxx

Asked

Viewed 132 times

3

I get that date from the comic book:

0.002976190476190476

I would like to move to this format: dd/mm/yyyy, using javascript pure 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), _
                                                strsql)
                set TopDB = nothing 

And here is the call to the js method, which is inside a while on Asp:

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

And that’s the js function that should do what I want, print a label at 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;
    else
        document.getElementById('lblPrazoSubPrestador').innerHTML = "";
}
  • 2

    First you have to identify which logic behind that numeric value represents the date.

  • 2

    I could not find anything that would return me this number with the logic UNIX Timestamp, only returns me 1970. But that’s what @Viniciusdutra really said, finding the logic of this number to do.

  • @Ruggi, I have no idea how this number is formed. It is a date on the Oracle. Dei um to_char(data, 'dd/mm/yyyy'), but it still didn’t work. This select is in a Classic ASP function. I will edit the post and post the function and select it.

  • maybe this value is a Javascript timestamp, more details in this link, caso seja isso mesmo, fica fácil fazer o que você quer:&#xA;ficaria assim: &#xA;&#xA;var data = new Date(valorNumericoDoBanco);&#xA;var dataFormatada = (data.getDate() > 9 ? data.getDate() : '0' + data.getDate() ) + '/' + (data.getMonth() > 9 ? data.getMonth() : '0' + data.getMonth() ) + '/' + data.getFullYear();

  • When I removed from Asp the tag <script>, the date entered normally, but now the javascript function no longer performs.

  • So dude, I don’t know much about Sp itself, more about Javascript, and it looks like your Javascript isn’t wrong. You tried to see the logic that @Viniciusdutra quoted?

  • Managed to solve?

Show 2 more comments

1 answer

2

The problem may be in this stretch

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

You are passing the parameter without quotation marks. Since the value is numeric and contains the math character of division /, a mathematical operation is taking place at the moment the parameter is entered in the function invocation rsOperadora().

To solve, enter the parameter delimited by quotation marks to be treated as a string.

Suggestion:

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

Only delimited with single quote. This ensures that the value is read as string.

BS: I’m not sure that’s what it is because the information in the question is confusing and vague.

Explanation of how I came to this deduction

Deducted due to value format 0.002976190476190476. The first thing I imagined is, how did you manage to get a date to reach this format?

By logical deduction I imagined 20/01/2016 (the date of today as an example). 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 "states" that the value already comes from the database. But looking at the query query, it made no sense. But when observing this part of the code & rsOperadora("dat_exclusao") &, made sense where the problem might be.

Checking out

To make sure about what I 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>, however, without quotation marks. Thus, without quotation marks, a mathematical operation is occurring. Resulting in the fractional value.

Browser other questions tagged

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