Hi , I want convert on timezone to another timezone .

p_jaya
Kilo Contributor

Here i am using moment Js for   conversion.   First i have copied the code from http://momentjs.com/timezone/   and paste in ui scripts. Then i have used ui page client script to call the moment function.   here is my code  

var now=moment().tz();

  alert('current time:'+now);

      var d=moment.tz(now,"Chile/EasterIsland").format('YYYY-MM-DD HH:mm:00');

  alert('converted time : '+d);

But it is not working.

My requirement is user will give time and zone . According to that   i want to convert that time into IST.

8 REPLIES 8

rahulpandey
Kilo Sage

HI,


Here you go




// Get date from service now  


      var time = new GlideDateTime(current.u_start_date);  


      // Display date retrieved  


      gs.print('GMT Time: '+time);  


      // Set timezone  


      var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");  


      time.setTZ(tz);  


      // Get offset of timezone set above  


      var timeZoneOffSet = time.getTZOffset();  


      // Add offset to current time  


      time.setNumericValue(time.getNumericValue() + timeZoneOffSet);  


      // Print date/time in desired timezone  


      gs.print('Eastern time: '+time);  



thanks to tim.harris


Mark this post as answered if it is correct.


p_jaya
Kilo Contributor

Hi Rahul,


In the line:


  var time = new GlideDateTime(current.u_start_date);


when i am using this line servicenow is taking timezone of system.


Like in my Ui page i have some list of time zones user can select from. So using the time zone selected by user as parameter i need the stored value in "var time" and zone   to change dynamically.


so can you modify the code to take the parameter instead of current time and include zone?


Hi,


I am not clear with the requirement however, you can convert the current time of any field and using any timzone(in your case list).


var time_field = gel('your date field').value;


var selected_timezone = gel('your time zone select field').value;


var time = new GlideDateTime(time_field); // it is the date field, which you want to change according to timezone selected in UI page


var tz = Packages.java.util.TimeZone.getTimeZone(selected_timezone);   // here, instead of "America/New_York", you can use the list value of timezone from your UI page.


var timeZoneOffSet = time.getTZOffset();


time.setNumericValue(time.getNumericValue() + timeZoneOffSet);


alert("changed time zone is "+time);