Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!