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

Conversion of date time GMT to a local time zone

  • January 19, 2024
  • 4 replies
  • 2260 views

I have used the below script to convert a date time field to local time zone. But, it is not giving the expected result when I use it in Adobe Livecycle Designer.

var gmtDateString = "2024-01-16T18:24:56";

 

// Parse the GMT date string
var gmtDate = Date(gmtDateString);

 

// Get the local timezone offset
var localTimeZoneOffset = Date("1900-01-01T00:00:00") - Date("1900-01-01T00:00:00UTC");

 

// Adjust the date by adding the local timezone offset
gmtDate = DateAdd(gmtDate, 0, 0, 0, 0, localTimeZoneOffset, 0);

 

// Manually construct the adjusted date string for the local timezone
var localDateString = gmtDate.getFullYear() & "-" &
                     padZero(gmtDate.getMonth() + 1) & "-" &
                     padZero(gmtDate.getDate()) & "T" &
                     padZero(gmtDate.getHours()) & ":" &
                     padZero(gmtDate.getMinutes()) & ":" &
                     padZero(gmtDate.getSeconds());

 

// Function to pad single-digit values with zero
function padZero(value) {
return value < 10 ? '0' + value : value;
}

 

this.rawValue = localDateString;

 

 

It is giving only the correct time, but the date is coming wrong as 15th August 1924. Could anyone please help and guide here.

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

4 replies

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
January 19, 2024

It seems like the issue might be related to how the local time zone offset is calculated and applied. Instead of manually calculating the offset, you can use the getTimezoneOffset method to get the local time zone offset and then adjust the date accordingly. Something like this:

var gmtDateString = "2024-01-16T18:24:56"; // Parse the GMT date string var gmtDate = new Date(gmtDateString); // Get the local timezone offset in minutes var localTimeZoneOffset = gmtDate.getTimezoneOffset(); // Adjust the date by adding the local timezone offset gmtDate.setMinutes(gmtDate.getMinutes() - localTimeZoneOffset); // Format the adjusted date string for the local timezone var localDateString = gmtDate.toISOString().slice(0, 19).replace("T", " "); this.rawValue = localDateString;

 

Hope this helps

Esteban Bustamante
MandeepSAuthor
Level 2
January 19, 2024

 

I tried it. Unfortunately, it didn't work and I am getting a blank value. Could you please let me know if I need to change anything in the Patterns section of the field object pallet in Adobe Livecyle Designer.

arunpatidar
Community Advisor
Community Advisor
January 19, 2024

HI @mandeeps 
Could you please try

 

// Function to convert GMT date string to local time zone function convertToTimeZone(gmtDateString) { // Parse the GMT date string var gmtDate = util.scand("yyyy-MM-ddTHH:mm:ss", gmtDateString); // Get the local time zone offset in minutes var timeZoneOffset = util.localTimezoneOffset(); // Apply the offset to get the local date var localDate = util.add(gmtDate, 0, 0, 0, 0, timeZoneOffset); // Format the local date as a string var localDateString = util.printd("yyyy-MM-ddTHH:mm:ss", localDate); return localDateString; } // Example usage var gmtDateString = "2024-01-16T18:24:56"; var localDateString = convertToTimeZone(gmtDateString); // Output the result xfa.host.messageBox("Local Time: " + localDateString, "Time Conversion", 3);
Arun Patidar
MandeepSAuthor
Level 2
January 19, 2024

I tried it. Unfortunately, it didn't work and I am still getting a blank value. Could you please let me know if I need to change anything in the Patterns section of the field object pallet in Adobe Livecyle Designer.

Raja_Reddy
Community Advisor
Community Advisor
January 19, 2024

Hi @mandeeps 
Try below code 

var gmtDateString = "2024-01-16T18:24:56"; // Create a Date object from the GMT date string var gmtDate = util.scand("yyyy-mm-dd'T'HH:MM:ss", gmtDateString); // Get the local time zone offset in minutes var localTimeZoneOffset = util.timezoneOffset(gmtDate); // Adjust the date by adding the local time zone offset var localDate = util.date.add(gmtDate, "n", localTimeZoneOffset); // Format the local date as a string var localDateString = util.printd("yyyy-mm-dd'T'HH:MM:ss", localDate); // Set the result to the form field this.rawValue = localDateString;
MandeepSAuthor
Level 2
January 22, 2024

Hi,

I tried the script. I also changed the pattern as below. Still I cannot see the date string calculated. It is still blank. Please suggest if I am missing something.

 

arunpatidar
Community Advisor
Community Advisor
January 22, 2024

Hi @mandeeps 
Can you try to print/debug the values.
check the field(id, name) where you are setting the value and compare with the code

Arun Patidar
kautuk_sahni
Community Manager
Community Manager
January 23, 2024

@mandeeps Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni