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

258
Visualizações
JS split(", ") works in Browser console but not in Node

I'm using Firefox, and tried this with node v14 and v17. I'm trying to parse a string into a date:

const extractThreadDate = (page: string): Date => {
      const $ = cheerio.load(page)
      const posthead = $("div .posthead")
      const postdate = $(".postdate", posthead)
      // format: 25/01/2010, 12:37
      const textDate = postdate.first().text().trim()
      console.log("1", textDate)
      const [dayDate, hourDate] = textDate.split(", ")
      console.log("2", [dayDate, hourDate])
      const [day, month, year] = dayDate.split("/").map(s => Number(s))
      console.log("3", [day, month, year])
      const [hour, minute] = hourDate.split(":").map(s => Number(s))
      console.log("4", [hour, minute])
      const date = new Date(year, month, day, hour, minute)
      return date
}

However the logs show the following:

1 25/01/2010, 12:37
2 [ '25/01/2010, 12:37', undefined ]
3 [ 25, 1, NaN ]

Any hints?

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

0

You can regex all whitespace using /\s/g in JavaScript, so to match this, you could do .split(/,\s/g) rather than targeting arbitrary/specific whitespace characters.

about 4 years ago · Juan Pablo Isaza Relatório

0

So, I did a sanity check and noticed that "25/01/2010, 12:37".split(", ") works correctly by itself. Weirded out, I put a fs.writeFileSync("hehehehe", textDate). Turns out there was a \u00a0 character instead of a space. Amazing. Who thought it'd be a good idea to have another whitespace? Whatever, that was it, split(",\u00a0") works fine.

The reason it worked is that when you copy it to the clipboard, it apparently gets converted into a regular whitespace.

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