Expand my Community achievements bar.

Conversion of date time GMT to a local time zone

Avatar

Level 2

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.

8 Replies

Avatar

Community Advisor

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

Avatar

Level 2

 

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.

Avatar

Community Advisor

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

Avatar

Level 2

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.

Avatar

Community Advisor

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;


Avatar

Level 2

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.

MandeepS_0-1705911971201.pngMandeepS_1-1705911990800.png

 

Avatar

Community Advisor

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

Avatar

Administrator

@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