Home > other >  Changing JSON date format received from API. ReactJS
Changing JSON date format received from API. ReactJS

Time:02-17

would appreciate any help on this. I am using this Screenshot of documentation - Openweather API

let currentTimestamp = Date.now()
console.log(currentTimestamp); // get current timestamp
let date = new Intl.DateTimeFormat('en-US', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' }).format(currentTimestamp)

console.log(date)

Intl.DateTimeFormat allows you to specify exactly which format would you want the Unix date in your application to be converted to.docs

You can also specify if you want to convert the Unix date to just time or date as well

//Just Date
let currentTimestamp = Date.now()
let date = new Intl.DateTimeFormat('en-US', { year: 'numeric', month: '2-digit', day: '2-digit' }).format(currentTimestamp) // 01/11/2021
console.log(date)

//Just Time
let currentTimestamp = Date.now()
let date = new Intl.DateTimeFormat('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit' }).format(currentTimestamp)//9:27:16 PM
console.log(date)

  • Related