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

219
Visualizações
How do I make a date with new Date() in javascript given information from an array in php?

I am iterating through an array of elements in php and accessing each element through javascript. I want to compare dates, but in order to do that I have to take out the information I need to do a new Date() object. This is what the information looks like in php:

"0": {
.
.
.
"dateInfo": {
  "date": "2021-11-19 00:00:000000",
  "timezone_type": 3,
  "timezone: "UTC"
  },
.
.
.
},

I am accessing these from an array in javascript by doing:

array["0"]["dateInfo"]

I tried to take the just the year month and date and make that into a new Date() object by doing this:

var parts = array["0"]["dateInfo"]["date"].split('-');
var dateToComp = new Date(parts[0], parts[1], parts[2]);

But when I did a console.log(dateToComp); it said it was an invalid date. My end goal to to compare this date with todays date to get something in the fashion:

var today = new Date()
var parts = array["0"]["dateInfo"]["date"].split('-');
var dateToComp = new Date(parts[0], parts[1], parts[2]);

if(dateToComp < today)
{
   return 0; // in the past
}
else
{
   return 1; // same day or future day
}

I hope what I am trying to say makes sense and any help would be much appreciated. Thank you.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

But when I did a console.log(dateToComp); it said it was an invalid date.

Because parts[2] is "19 00:00:000000", which is an invalid date component. Instead split on any non–digit character \D. Also, you need to subtract 1 from the month.

E.g.

let date = '2021-11-19 00:00:000000';
// Split on any non–digit character
let [y, m, d] = date.split(/\D/);
// Months are zero indexed
// Treat input as local
console.log(new Date(y, m-1, d).toString());
// Treat input as UTC
console.log(new Date(Date.UTC(y, m-1, d)).toISOString());

about 4 years ago · Juan Pablo Isaza Relatório

0

var dateToComp = new Date(parts[0], parts[1], parts[2].split(' ')[0]);
about 4 years ago · Juan Pablo Isaza Relatório

0

new Date(array["0"]["dateInfo"]["date"])

should work

about 4 years ago · Juan Pablo Isaza 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