convert eastern time to gmt in javascript

To convert Eastern Time to GMT in JavaScript, you can use the toLocaleString() method of the Date object along with the timeZone parameter.

Here's an example code snippet:

index.tsx
const currentDate = new Date();
const etTime = currentDate.toLocaleString('en-US', { timeZone: 'America/New_York' });
const gmtTime = currentDate.toLocaleString('en-US', { timeZone: 'GMT' });

console.log(`Current Eastern Time: ${etTime}`);
console.log(`Current GMT Time: ${gmtTime}`);
286 chars
7 lines

In the above example, currentDate is the current date and time in the local timezone of the user's browser. We then use the toLocaleString() method to format the date and time string with the timeZone parameter set to 'America/New_York' for Eastern Time and 'GMT' for GMT. This returns the date and time in the respective timezones as formatted strings, which we log to the console.

Note that you can replace currentDate with any valid Date object to convert any date and time from Eastern Time to GMT.

gistlibby LogSnag