convert gmt to new zealand time in javascript

To convert GMT to New Zealand time in JavaScript, you can use a library like moment.js to handle timezone conversions. Here's an example code block:

index.tsx
// Set the date in GMT format
let gmtDate = moment.utc('2021-10-28 10:30:00');

// Convert to New Zealand time zone
gmtDate.tz('Pacific/Auckland');

// Format the date in New Zealand time zone
let nzTime = gmtDate.format('YYYY-MM-DD HH:mm:ss');

console.log(nzTime); // Output: 2021-10-28 23:30:00
298 chars
11 lines

In the code above, we first set the date in GMT format using moment.utc(). Then, we use the tz() function to convert the date to the New Zealand time zone. Finally, we format the date in the desired output format using the format() function.

gistlibby LogSnag