How to treat Timestamp field return via AJAX?

Asked

Viewed 137 times

1

How to convert this sequence of numbers: 1555506548000 into a valid date ?

I need to make the following comparison:

if (_valor[0] == item.DT_CRIADO_EM) {
    //Faz alguma coisa
};  

However the date is not in the desired format as described above. It follows the relevant code snippet:

Response from AJAX

success: function (response) {

    $.each(response.emailList, function (index, item) {

        console.log("item.DT_CRIADO_EM: " + item.DT_CRIADO_EM);  // <<---  item.DT_CRIADO_EM: /Date(1555506548000)/

        $("input[name='txtData[]']").each(function () { 
            var _valor = new Array();
            _valor.push($(this).data("dt_criado_em"));

            console.log("_valor[]: " + _valor[0]);               // <<---  _valor[]: 15/04/2019 00:00:00

            if (_valor[0] == item.DT_CRIADO_EM) {                // <<--- Preciso comparar as duas datas aqui
                //Faz alguma coisa
            };                              
        }); 

    });
},

inserir a descrição da imagem aqui

  • Possible duplicate of Convert timestamp value to date

  • Download this Datelocale.js plugin ?

  • pf read the answer

  • Excuse my ignorance but I need more help, I have read the answer and implemented the code and gives error lock visual studio: var date = new Date(1555506548000 * 1000);

  • The value of item.DT_CRIADO_EM is exactly this (a string with bars and everything): /Date(1555506548000)/?

  • Exactly @Sam ! I updated the post and added a console print.

  • do not need to download the plugin, just use the code that is in the answer: var data = new Date(1555506548000).toLocaleDateString("pt-BR"), and if you want to validate, only one value alert(data);

Show 2 more comments

2 answers

2


How everything is strings (the value in item.DT_CRIADO_EM and the value of the array’s first position _valor[]), you can pick up the timestamp on item.DT_CRIADO_EM and convert to date. Catch also the date on _valor[0], everything with .match() using regular expressions.

Then just convert the two with .toLocaleDateString("pt-BR"), resulting both dates in format dd/mm/aaaa, and make the comparison on if (whereas you want to compare only if one date is equal to another):

var item = {
   DT_CRIADO_EM: "/Date(1555506548000)/"
}

var _valor = ['15/04/2019 00:00:00'];

// trata da data em _valor[0]
var valordata = _valor[0].match(/\d+\/\d+\/\d+/)[0].split("/");
var valordia = new Date(valordata[2], valordata[1]-1, valordata[0]).toLocaleDateString("pt-BR");

// trata da data em item.DT_CRIADO_EM
var criadoemdia = new Date(Number(item.DT_CRIADO_EM.match(/\d+/)[0])).toLocaleDateString("pt-BR");

console.log(valordia, criadoemdia);

if (valordia === criadoemdia){
   console.log("datas iguais");
}else{
   console.log("datas diferentes");
}

  • Perfect @Sam ! Worked as expected ! Thank you !

1

var timestamp = 1555506548000; // ou Date.now();
var date = new Date(timestamp);
var localDateString = date.toLocaleDateString("pt-BR"); // 17/04/2019

Can test here.

Browser other questions tagged

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