Get Local Time From UTC Timestamp In JavaScript
JavaScript
13/02/2023
If you've got a UTC timestamp string,
JAVASCRIPT
const utcString = new Date().toISOString()console.log(utcString) // "2022-10-30T15:15:41.471Z"
turn it into a Date object, and JavaScript will automatically convert it to your local timezone.
JAVASCRIPT
const localTime = new Date(utcString)console.log(localTime) // `Date` Sun Oct 30 2022 16:15:41 GMT+0100 (Mitteleuropäische Normalzeit)
For a string representation of the date, use the toLocaleString
method.
JAVASCRIPT
const localeString = localTime.toLocaleString()console.log(localeString) // "30.10.2022, 16:15:41"