convert gmt to new zealand time in typescript

To convert from GMT to New Zealand time in TypeScript, you can use the toLocaleString() method of the Date object along with the timeZone option. Here's an example:

index.ts
const gmtDate = new Date('2022-06-30T16:30:00.000Z');
const nzDate = gmtDate.toLocaleString('en-NZ', { timeZone: 'Pacific/Auckland' });
console.log(nzDate); // Output: "01/07/2022, 4:30:00 AM"
193 chars
4 lines

In this example, we first create a Date object representing the GMT time using the Date() constructor and passing in the ISO string for the date and time. The Z at the end of the string indicates that the time is in GMT.

We then call the toLocaleString() method on the gmtDate object, passing in the 'en-NZ' locale and the timeZone option with the value 'Pacific/Auckland'. This will format the date and time in the New Zealand time zone and return a string with the appropriate format.

Finally, we log the formatted date and time to the console.

gistlibby LogSnag