Expand my Community achievements bar.

SOLVED

Conversion of date time GMT to a local time zone

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.



View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

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.



Avatar

Level 2

Thanks for a quick response.

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

Avatar

Community Advisor

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.



Avatar

Level 2

Hi Raja, I tried your approach and wrote the below code. The same is working fine on any online javascript compiler. But, when I am writing it in adobe livecycle designer it is not performing the calculation and not showing the correct date time. Still shows 1 hour behind. Could you please suggest if I have missed something.

MandeepS_0-1705493781572.png