0

would appreciate any help on this. I am using this API and the data is returned in JSON format. However, the date/time is returned as a value similar to this: dt: 1645068034 How can I convert this to a readable format in my react project?

2 Answers 2

2

Here's an option for you:

new Date(1645068034 * 1000)
Sign up to request clarification or add additional context in comments.

Comments

0

I found the solution for this in this site Cloudhadoop-React-timestamp. Feel free to refer to this for additional information.

The date-time format that you are getting from the API mentioned in your question is in the Unix format. 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.