Conversion of date time GMT to a local time zone | Community
Skip to main content
Level 2
January 16, 2024
Solved

Conversion of date time GMT to a local time zone

  • January 16, 2024
  • 1 reply
  • 6614 views

Hello,

 

Please let me know the JS or FormCalc script code snippet for getting setting a date time field in GMT format to  the local time zone.

 

Thank you

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Raja_Reddy

Hi @mandeeps 
Assuming you have a date string in GMT format and you want to convert it to the local time zone

// Assuming you have a date string in GMT format var gmtDateString = "2024-01-16T18:24:56Z"; var gmtDate = new Date(gmtDateString); var localTimeZoneOffset = new Date().getTimezoneOffset(); gmtDate.setMinutes(gmtDate.getMinutes() - localTimeZoneOffset); // Example: Display the adjusted date in a specific format var localDateString = gmtDate.toLocaleString(); console.log(localDateString);

This JavaScript code snippet takes a date string in GMT format, converts it to a Date object, adjusts it to the local time zone by subtracting the local time zone offset, and then formats it as a local date string.

1 reply

Raja_Reddy
Community Advisor
Raja_ReddyCommunity AdvisorAccepted solution
Community Advisor
January 16, 2024

Hi @mandeeps 
Assuming you have a date string in GMT format and you want to convert it to the local time zone

// Assuming you have a date string in GMT format var gmtDateString = "2024-01-16T18:24:56Z"; var gmtDate = new Date(gmtDateString); var localTimeZoneOffset = new Date().getTimezoneOffset(); gmtDate.setMinutes(gmtDate.getMinutes() - localTimeZoneOffset); // Example: Display the adjusted date in a specific format var localDateString = gmtDate.toLocaleString(); console.log(localDateString);

This JavaScript code snippet takes a date string in GMT format, converts it to a Date object, adjusts it to the local time zone by subtracting the local time zone offset, and then formats it as a local date string.

MandeepSAuthor
Level 2
January 16, 2024

Thanks for a quick response.

However, if I want to convert from a CET to local timezone. Is that also possible?

Raja_Reddy
Community Advisor
Community Advisor
January 16, 2024

yes,it is definitely possible to convert a date from CET (Central European Time) to the local timezone using JavaScript. You can achieve this by explicitly setting the timezone offset for CET and then adjusting the date accordingly

var cetDateString = "2024-01-16T18:24:56+01:00"; // Example CET date string // Create a Date object using the CET date string var cetDate = new Date(cetDateString); // Get the local timezone offset var localTimeZoneOffset = new Date().getTimezoneOffset(); // Calculate the offset between CET and local timezone var cetTimeZoneOffset = 60; // CET is UTC+1 // Adjust the date by subtracting the timezone offsets cetDate.setMinutes(cetDate.getMinutes() + cetTimeZoneOffset - localTimeZoneOffset); // Example: Display the adjusted date in a specific format var localDateString = cetDate.toLocaleString(); console.log(localDateString);

 cetTimeZoneOffset is set to 60 minutes, representing the UTC+1 offset for CET. Adjust this value based on the specific time zone offset for CET in your scenario.