Updating date/time field based on time zones

CandyDee
Kilo Sage

Got an interesting one. Picking through an old business rule and script from the team who implemented the platform.

When an incident is logged, it makes the public correspondence field available. When the public correspondence field is updated it updates the 'first' field with the current time/date and then any further updates the 'last correspondence' time is updated each time. Both are working fine.

My issue is that its showing the updated time and date as in 8 hours in the future which I'm guessing is due to a time zone. so i logged the incident at 10:30am which is correct in terms of time stamp for the incident. 

Trying to figure out if its possible to change the update time to match my time zone or even if I should. I have included the BR and script include as well which I think is running to update those fields.

 

 

find_real_file.png

 

BUSINESS RULE

try{
if(gs.hasRole("itil")){
//only run business rule for DC user updates
var tcf;
var timeStamp;
var first, last; //hold the glide date time objects for the first and last comment time stamp fields

tcf = new TaskCorrespondenceFactory(current.comments.getJournalEntry(-1));
lastTimeStamp = tcf.getLastDCCorrespondenceTimeStamp();
now = (new Date()).getTime(); //in ms

gs_now = gs.nowDateTime();

first = current.u_first_dc_comment_time_stamp.getGlideObject();
last = current.u_last_dc_comment_time_stamp.getGlideObject();

//check if the last update was from the current user and was <= 1 minute ago
//gs.print("NOW == " + now);
//gs.print("GS NOW ==" + (new Date(gs_now)).getTime());
//gs.print("LAST TIME STAMP == " + lastTimeStamp);
//gs.print("DIFF == " + (now - lastTimeStamp));
if (now - lastTimeStamp <= 10000){
//allow a generous 10 seconds for the business rule to run after the task update
//set that time as the last correspondence time
last.setNumericValue(lastTimeStamp);
//check if the the first correspondence field is empty
if(first.toString() == ""){
//this is the first correspondence from DC to the client
first.setNumericValue(lastTimeStamp);
//since this is the first correspondence from DC, we can make the task visible to the client if it's already invisible
if (current.u_is_client_visible == false){
current.u_is_client_visible = true;
}
}
//moving script to kick off before the record is updated since there is a bug (duplication issue) when running after record update
//current.update();
}

}
}
catch(e){
gs.print("ERROR: " + e);
}

 

SCRIPT INCLUDE

var TaskCorrespondenceFactory = Class.create();
TaskCorrespondenceFactory.prototype = Object.extendsObject(AbstractAjaxProcessor, {
initialize: function(journalEntriesRaw) {
this.journalEntries = journalEntriesRaw.split("\n\n");
this.headerPattern = RegExp("(\\d+)-(\\d+)-(\\d+) (\\d+):(\\d+):(\\d+) (.*)Public Correspondence.*");
this.lastDCTimeStamp = null; //last time (in seconds) a DC user wrote to the client
this.lastDCuser = null; //user name that last wrote to the client
},

isDCUser: function(userName) {
//check if the given user is a DC user
return true;
},

hasDCCorrespondence: function() {
//check if there is at least one correspondence from a DC user
},

getLastDCCorrespondenceTimeStamp: function() {
//convert the time stamp from the raw function to milliseconds
return (this.getLastDCCorrespondenceTimeStampRaw().getTime());
},
getLastDCCorrespondenceTimeStampRaw: function() {
//get the time stamp of the last correspondence by a DC user
var yr, mo, day, hr, min, sec;
var userName;
var timeStamp;
for (var i = 0; i < this.journalEntries.length - 1; i++) {
if (this.headerPattern.exec(this.journalEntries[i])) {
//timeStamp = (new Date(RegExp.$1, RegExp.$2, RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6)).getTime() / 1000;
timeStamp = new Date(RegExp.$1, (RegExp.$2 - 1), RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6, 0);
userName = RegExp.$7;
//valid DC user?
if (this.isDCUser(userName)) {
this.lastDCUser = userName;
this.lastDCTimeStamp = timeStamp;
return timeStamp;
}
}
}

//if we're here, there was no correspondence from DC
return 0;
},
isLastDCCorrespondenceInDuration: function(duration) {
//using the given duration (assumed in seconds), check if the last correspondence time has exceeded that duratio
var now = (new Date()).getTime() / 1000;
if (now - this.lastDCTimeStamp >= duration) {
return false;
} else {
return true;
}
}

});

 

1 REPLY 1

Chinmay Tawade1
Tera Guru
This below script will return date and time according to TimeZone.
The only thing you will need is time zone abbrevation
 
var time = new GlideDateTime();
gs.info('GMT time is - ' + time);
var targetTimezone = 'IST'; // Must Specify Correct Timezone Abbreviation
var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone);
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
gs.info('IST time is - ' + time);//here time variable will give you the date and time according to Timezone