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

271
Visualizações
JS file not formatting date column in csv download

I have a JS file that is called in a python-based bokeh app. The JS file produces a download button to get a csv of the data used in the interactive charts made by the app. The data contain dates and integers. How can I get the downloaded csv to include the formatting for the dates?

This is the JS file.

function table_to_csv(source) {
  const columns = Object.keys(source.data)
  const nrows = source.get_length()
  const lines = [columns.join('`').replaceAll(/,/g,' ').replaceAll(/`/g,',')]
  
  for (let i = 0; i < nrows; i++) {
    let row = [];
    for (let j = 0; j < columns.length; j++) {
      const column = columns[j]
      const val = source.data[column][i]
      if (column == 'Date'){
        row.push(val.getDate() +'/'+(val.getMonth()+1)+'/'+val.getFullYear()}
      else
        row.push(val.toString())
    }
    lines.push(row.join(','))
  }
  return lines.join('\n').concat('\n')
}


const filename = 'data/survey_results.csv'
const filetext = table_to_csv(source)
const blob = new Blob([filetext], { type: 'text/csv;charset=utf-8;' })

//addresses IE
if (navigator.msSaveBlob) {
  navigator.msSaveBlob(blob, filename)
} else {
  const link = document.createElement('a')
  link.href = URL.createObjectURL(blob)
  link.download = filename
  link.target = '_blank'
  link.style.visibility = 'hidden'
  link.dispatchEvent(new MouseEvent('click'))
}

The date format in the csv used for the charts is d/m/y.

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

0

It took me a while to work out, but (of course) the answer was straightforward. I changed line 12 to:

row.push(new Date(val).toLocaleDateString("en-US"))

One problem was I had misunderstood where the data was coming from. It was from bokeh CDS, with the dates as standard yyyy-mm-ddT00:00:00.000000000.

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