Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
"Invalid Date" for User Provided 64 Bit Unix Timestamp

I am working on a JavaScript application to convert a 64 bit unix time string to a logical date. I found a pretty good tutorial over at "Geeks for Geeks" and the code works fine as long as there is a hardcoded value. However, when I change the variable to accept a user value I am getting "invalid date."

The value provided and the value hard coded are the same, so I'm lead to believe I'm referencing the input or failing to convert it to another variable type. Any explanation is greatly appreciated.

Basically, this code works:

            var mSeconds = 1638000000000;

This code returns "invalid date"

            var mSeconds = inputDate;

Even though the value is the same.

var inDate = document.getElementById('inputDate').value;
var outDate = document.getElementById('displayDate');
<!-- The below value is hard coded, which works-->
var mSeconds = 1638000000000;
<!-- The below value is commented out, but returns "invalid date" otherwise -->
<!-- var mSeconds = inputDate-->
inDate.innerHTML = "Entered Value = " + mSeconds;

function convert() {
  var date = new Date(mSeconds);
  outDate.innerHTML = date.toString();
}
<h1>64 Bit Unix Time Converter</h1>

<input type='text' id='inputDate'>
<button onClick="convert()">Convert</button>

<p id="displayDate"></p>

<p>Test Value 1: 1638000000000</p>
<p>Test Value 2: 1658000000000</p>
<!-- These are just values that I wanted handy to test the application -->

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You have two issues in your code:

  1. The input value retrieval should be done in the convert method. As it's currently done, you retrieve the initial value of the input and use it everytime in your convert method

  2. You have to parse the input value as an integer to call the correct Date constructor.

function convert() {
  var inDate = document.getElementById('inputDate').value;
  var outDate = document.getElementById('displayDate');

  var mSeconds = parseInt(inDate)
  var date = new Date(mSeconds);
  outDate.innerHTML = date.toString();
}
<h1>64 Bit Unix Time Converter</h1>

<input type='text' id='inputDate'>
<button onClick="convert()">Convert</button>

<p id="displayDate"></p>

<p>Test Value 1: 1638000000000</p>
<p>Test Value 2: 1658000000000</p>
<!-- These are just values that I wanted handy to test the application -->

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda