Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
Convert date in timestamp to DD/MM format inside forEach

Is possible to convert date in timestamp format to get day and month in format DD/MM ?

 result.forEach(obj =>
    html += `Temperature: ${obj.temp} ºC<br>
    Day: ${obj.dt}<br>
    Description: ${obj.description}<br>
    ${iconCodes[obj.icon]}<br><br>`
  );
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I saw from your previous question that obj.dt is a timestamp. It's a normal timestamp (number of seconds since the epoch) so we'll need to multiiple by 1000 to make it a timestamp javascript understands (it counts in millliseconds). You can easily modify that into the format you want by converting it into a date object, then using methods to extract the day and month. To keep everything to 2 digits, I used slice()

let result = [
  {
    dt: 1643884851,
    temp: 8.11,
    description: 'few clouds',
    icon: '02d'
  },
  {
    dt: 1643889600,
    day: 9.56,
    description: 'scattered clouds',
    icon: '03d'
  }
]

const getTimeFrom = d => {
  d = new Date(d * 1000);
  console.log(d)
  let day = ('0' + d.getDate()).slice(-2);
  let month = ('0' + d.getMonth() + 1).slice(-2);
  return `${day}/${month}`;
}

html = '';
result.forEach(obj =>
    html += `Temperature: ${obj.temp} ºC<br>
    Day: ${getTimeFrom(obj.dt)}<br>
    Description: ${obj.description}<br>
    ${obj.icon}<br><br>`
  );
  
  document.querySelector('#output').innerHTML = html
<div id='output'></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another method

const timestamp = 1643916638401
const formattedDate = new Date(timestamp).toLocaleString('en-GB').substr(0,5);
console.log(formattedDate)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda