converting specified time/date field to the user specific timezone

burmakranthi
Tera Contributor

I have 'appointment time' field on the incident form.This value will be displayed in the emails sent to the user

My question is :how to convert this time to the users timezone and display it in the emails

2 REPLIES 2

prashantnagaral
Kilo Guru

Hi Rekha,



I suggest you to create a below email script to convert the appointment time to user timezone. Hope this help you.



Create a script include with the name TimeUtils and paste the below code.



TimeUtils.changeTimeZone = function (object, prop, timeZone) {  


    if (typeof object != "object") {  


//Custom logger used ... You can change this to gs.log  


              Logger.error('Invalid object passed in to TimeUtils.changeTimeZone(): ' + typeof object, 'TimeUtils');  


              return false;  


      }  


      try {  


              if (!JSUtil.nil(prop) && !JSUtil.nil(timeZone)) {  


                      var tz = Packages.java.util.TimeZone.getTimeZone(timeZone);  


                      object[prop].getGlideObject().setTZ(tz);  


              }  


      } catch (ex) {  


//Custom logger used ... You can change this to gs.log  


              Logger.error("Exception at TimeUtils.changeTimeZone: " + ex, 'TimeUtils');  


              return false;  


      }  


      return true;  


};  



You can call the above script include in email script to convert the 'appointment_time' to user timezone.



<mail_script>


    var user = gs.getUser();


    var timeZone = String(user.getTZ());   You can change this line to get the user you wish to use the time zone for or you can use a hard coded time zone.e.g: 'US/Eastern'


    TimeUtils.changeTimeZone(current, 'appointment_time', timeZone);


    template.print(Appointment Date: ' + current.appointment_time.getDisplayValue());


</mail_script>



Thanks


Prashant


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Rekha,



In the email script directly use getDisplayValue() on that field/variable this will show the date/time value in the user timezone.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader