How to convert GMT date to EST

jesusemelendezm
Mega Guru

When importing expirydate from LDAP to sys_user table - I am obtaining an incorrect date. If user expiry date in Active directory is 31/12/2014 the date being displayed on the user form on ServiceNow is 01/01/2015 I was told that It is being imported on GMT and I need to convert it to EST. Can I do it directly on the date field?


Capture.JPG

1 ACCEPTED SOLUTION

It will only affect that one particular variable and field.   Your script is not quite right though as you need to modify the timezone for your gDate variable.   You need to put:



//converting GMT to Canada/Eastern


var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");


gDate.setTZ(tz);



...right after you initialize the variable and before you assign the date to it.     You should end up with:



var gDate = Packages.com.glide.glideobject.GlideDateTime();


//converting GMT to Canada/Eastern


var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");


gDate.setTZ(tz);


gDate.setValue(msDate);


View solution in original post

17 REPLIES 17

I have included the code on the on-before transform script. This is how It looks like.



Question:



Will this affect the rest of date fields on the sys_user table or any where in the system?



Thanks



J.Transform map script on before.JPG


No this won't affect anywhere, This will affect target.date, that is only date field.


It will only affect that one particular variable and field.   Your script is not quite right though as you need to modify the timezone for your gDate variable.   You need to put:



//converting GMT to Canada/Eastern


var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");


gDate.setTZ(tz);



...right after you initialize the variable and before you assign the date to it.     You should end up with:



var gDate = Packages.com.glide.glideobject.GlideDateTime();


//converting GMT to Canada/Eastern


var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");


gDate.setTZ(tz);


gDate.setValue(msDate);


Jim, the field u_expiry_date should be date/time type or date.



I have it as 01/01/2015 00:00:00 - which is date/time.


After running again - scheduled import from LDAP. It 's still displaying the value with an additional day.



// Updates the SN Account Expires field with the accountExpires value from AD  


// Active Directory stores the accountExpires date as an Integer8 which must be converted into DateTime for SN  



var n = source.u_accountexpires;  


var s = n.toString();  


 


// Only convert AD accountExpires values that begin with 1 (ex. 0 represents never expires)  


if (s.charAt(0) == 1) {  


    var msDate = new Packages.org.apache.poi.hpsf.Util.filetimeToDate(n);  


    var gDate = new Packages.com.glide.glideobject.GlideDateTime();  



//converting GMT to Canada/Eastern


     


var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");  


gDate.setTZ(tz);  


//converting GMT to Canada/Eastern


     


    gDate.setValue(msDate);  


    target.u_expiry_date = gDate;  


    gs.log('Setting the Account Expires date for ' + target.name + ' to ' + gDate);


     


     


     


}  


// Clear the SN Account Expires date if the accountExpires date is no longer set in AD  


else if (!target.u_expiry_date.nil()) {  


    target.u_expiry_date = '';  


    gs.log('Updating the SN Account Expires date for ' + target.name + ' to Never');  


}