Issue with timezone conversion with EST time zone.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2024 01:44 AM
Hello community
I have a requirement that in change form I have planned start date field when I select date and time in any time zone it should be converted into EST/EDT and display in another field which is planned start date (EST) field for this I have written these scripts
Script include:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2024 06:41 AM - edited 08-01-2024 06:59 AM
Hi @Hemanth Naik ,
i tried something like below it worked for me
Script include:
var ConvertToESTutil = Class.create();
ConvertToESTutil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
convertDates: function() {
var mydate = new GlideDateTime(this.getParameter('sysparm_date').toString());
var gd = new GlideDateTime();
var timeZone = gd.getUserTimeZone();
var timeZoneID = timeZone.getID();
if (timeZoneID != 'US/Eastern') {
var tz = Packages.java.util.TimeZone.getTimeZone('US/Eastern');
gd.setTZ(tz);
gd.setValue(mydate);
return gd.getDisplayValue();
} else {
return mydate;
}
},
type: 'ConvertToESTutil'
});
Please mark helpful & correct answer if it's really worthy for you.
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2024 10:34 PM - edited 08-01-2024 10:50 PM
Hi @Bhavya11 , thank you for your valuable response, The code mentioned is above working fine as expected for us/eastern time zone,
But can you please check by changing your time zone to IST then it should convert to 9 hrs. behind to show in Est right, but it will convert only 4 hrs. behind
The reason for these conversions I came to know that the calendar in instance will run in instance time zone, so it is expected behavior.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2024 11:15 PM
I had similar requirement and I got away with the following BR. You can modify it as per your use case-
var preferredSchedule = current.u_preferred_schedule.getGlideObject(); //This field stores time in Local timezone which is current.location.time_zone basically.
var time = new GlideDateTime(preferredSchedule);
var zoneName = current.location.time_zone;
var time2 = preferredSchedule.getDate();
var systemtimeZoneOffSet = time2.getTZOffset();
gs.log('Case Preferred Schedule GMT Time: ' + time);
// Set timezone
var tz = Packages.java.util.TimeZone.getTimeZone(zoneName);
time2.setTZ(tz);
var localtimeZoneOffSet = time2.getTZOffset();
time.addSeconds((systemtimeZoneOffSet / 1000) - (localtimeZoneOffSet / 1000));
// Print date/time in desired timezone
gs.log('Case Preferred Schedule Time is: ' + preferredSchedule);
current.u_preferred_schedule_sys = time; // replace custom_field with name of your date/time custom field. Here you will get time in IST.
Please mark my answer helpful and correct.
Regards,
Amit