How to Convert GMT to local time in JavaScript

To convert a GMT (UTC) time to local time in JavaScript, you can use the “Date” object and the toLocaleString()” method.

First, convert the GMT date string to a Date object using the “new Date()” constructor and then use the “toLocaleString()” method to convert it into the local time.

Example

// Create a GMT (UTC) date string
const gmtDateString = '2023-04-22T15:30:00Z';

// Convert the GMT date string to a Date object
const gmtDate = new Date(gmtDateString);

// The Date object will automatically be in the local time zone
console.log('Local Time:', gmtDate.toLocaleString());

Output

Local Time: 4/22/2023, 9:00:00 PM

In the above code example, we created a GMT (UTC) date string called “gmtDateString”.

In the next step, we created a Date object called gmtDate using the GMT date string.

Then, we used the “toLocaleString()” method to format the Date object as a string in the local time zone and logged the result to the console.

Remember that the system will determine the local time zone, so the output may vary depending on the user’s location and system settings.

Leave a Comment