how to convert glideDateTime that from GMT to EST using script include

ganesh7
Kilo Contributor

Hi All,

i want to write script include for converting GMT To EST, As

They are basically just Service Now's javascript class for working with date and time. They are a headache to use though, especially when it comes to time zones. Although the system time zone is set to Eastern Standard Time (EST), GlideDateTime objects are sometimes initiated in GMT (international time based out of London). To help with this we would like you to write the following script includes:

u_getLocTZ(gdt) -> takes GlideDateTime object and returns the object in the local time zone

u_getGMTTZ(gdt) —> takes GlideDateTime object and returns the object in the GMT time zone

u_isGMT(gdt) -> takes GlideDAteTime object and returns true if it is in the GMT time zone.

u_isLocTZ(gdt) -> takes GlideDateTime object and returns true if it is in the local time zone (EST).

could you please give some idea about this.

Regards,

Ganesh.

7 REPLIES 7

VaranAwesomenow
Mega Sage

var dateTimeUtils = Class.create();


dateTimeUtils .prototype = {


initialize: function() {


},



convertTaddmDate: function(inputDateTime) {


var datetime = new GlideDateTime();


try {


var year = '';


var month = '';


var day = '';


var time = '';



var sourceDateTime = inputDateTime;


sourceDateTime = sourceDateTime.substring(0,18);


var sourceDateTimeArray = sourceDateTime.split('/');


var sourceDateTimeNewArray = sourceDateTimeArray[2].split(' ');


year = sourceDateTimeNewArray[0];


if (sourceDateTimeArray[0].length <2) {


month = '0' + sourceDateTimeArray[0];


}


else {


month = sourceDateTimeArray[0];


}


day = sourceDateTimeArray[1];


time = sourceDateTimeNewArray[1];


sourceDateTimeNew = year + '-' + month + '-' + day   + ' ' + time;


datetime.setValue(sourceDateTimeNew);


var tz = gs.getSession().getTimeZone();


var dateMilliSecs = 0;


dateMilliSecs= datetime.getNumericValue();


var gdt = new GlideDateTime();


gdt.getLocalTime(); // local time


var offsetMilliSecs = 0;


offsetMilliSecs= Math.abs(gdt.getTZOffset());


var dateTimeMillisecs = 0;


dateTimeMillisecs = (dateMilliSecs - offsetMilliSecs);


datetime.setNumericValue(dateTimeMillisecs);


}


catch(e) {


gs.log('Error in Install Date format ' , 'TADDM Intergration');


}


return datetime;


},


type: 'dateTimeUtils '


};



##################################
Output




##################################


*** Script: Input 4/14/2011 6:54:34 AM (GMT)


*** Script: Return 2011-04-13 23:54:34 (PST) ( local time)



Thanks


Anil


Tushar Walveka2
Tera Guru

Try this

var strConvertedDateTime = new GlideScheduleDateTime(new GlideDateTime()).convertTimeZone("UTC", "EST");
var gdtConvertedDateTime = new GlideDateTime(strConvertedDateTime)

gs.info("Converted Time: "+gdtConvertedDateTime);

@Tushar Walveka2this. saved. my. life. !!!   Thank you!!