convert australian central time to gmt in typescript

To convert Australian Central Time to GMT in TypeScript, you can use the built-in Date object and its methods. Here is an example code snippet:

index.ts
// Set the date and time for Australian Central Time
const date = new Date('2021-09-27T13:30:00+09:30');

// Get the UTC time in milliseconds
const utcTime = date.getTime() - (date.getTimezoneOffset() * 60000);

// Create a new date object with the UTC time and format it as a string in GMT
const gmtTime = new Date(utcTime).toUTCString();

console.log(gmtTime); // Output: Mon, 27 Sep 2021 04:00:00 GMT
404 chars
11 lines

In this example, we set the date and time for Australian Central Time by passing the UTC offset of +9:30 to the Date constructor. Then, we get the UTC time in milliseconds by subtracting the time zone offset (in minutes) from the date's time value using the getTime() and getTimezoneOffset() methods.

Next, we create a new Date object with the computed UTC time value and format it as a string in GMT using the toUTCString() method. Finally, we log the output to the console.

Note that this code assumes that the input date string is in ISO 8601 format, which is supported by most modern browsers. However, if you need to support other date formats, you may need to use a third-party library such as Moment.js for parsing and formatting dates. Additionally, keep in mind that this code only converts a single date/time value and does not handle recurring events or daylight saving time changes.

gistlibby LogSnag