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.
Views
Replies
Total Likes
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
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.
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);
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.
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;
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 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.
Views
Replies
Total Likes
Views
Likes
Replies