Offset on GlideDateTime value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2023 06:09 AM
Hi All,
I am using below piece of code to retrieve the most recent date , it is working fine but whatever date is retrieved its time is either decreased by 1 hour or 2 hour and that is causing an issue:
var ga = new GlideAggregate('incident');
ga.addQuery('active=1');
ga.addAggregate('MAX', 'sys_created_on');
ga.setGroup(false);
ga.setOrder(false);
ga.query();
while (ga.next()) {
gs.log('Most recent Incident Created Date = ' + ga.getAggregate('MAX', 'sys_created_on'));
}
Example if the recent date is "12/07/2023 16:10:10" the retrieved date is : 12/07/2023 14:10:10
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2023 06:46 AM
The time in sys_created_on field is stored in UTC time zone
While the script you have written would be retrieving time in UTC and converting it to equivalent time in user's time zone.
The time difference in values is the difference between user time zone and UTC/GMT
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2023 11:06 PM
Hi @Manmohan K ,
Thanks for the reply.
I understand that time stored in table is in a fixed timezone , but how do I overcome this problem ?
Do I need to have a function converting the date-time value or I have tried below solution as well :
var ga = new GlideRecord('sc_req_item');
ga.addQuery('u_customer_item','a7837fe3f72aa910ec1c41b84851e0d6');
//ga.addAggregate('MAX', 'due_date');
//ga.setGroup(false);
//ga.setOrder(false);
var all_dates=[];
ga.query();
var i=0;
while (ga.next()) {
//gs.log('Most recent due date is = ' + ga.getAggregate('MAX', 'due_date'));
all_dates.push(ga.due_date.getDisplayValue());
gs.log('Dates are from record '+ga.due_date + 'from array ' + all_dates[i] + 'Display Value is ' + ga.due_date.getDisplayValue());
i++;
}
var largestdate = new GlideDateTime();
largestdate = max_date(all_dates);
var gritem = new GlideRecord('sc_req_item');
gritem.addQuery('sys_id','a7837fe3f72aa910ec1c41b84851e0d6');
gritem.query();
while(gritem.next())
{
gritem.due_date = largestdate;
gritem.update();
}
gs.log('Largest Date is '+ largestdate);
function max_date(all_dates) {
var max_dt = all_dates[0],
max_dtObj = new GlideDateTime(all_dates[0]);
all_dates.forEach(function(dt, index)
{
if ( new GlideDateTime( dt ) > max_dtObj)
{
max_dt = dt;
max_dtObj = new GlideDateTime(dt);
}
});
return max_dt;
}
Does this looks okay?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2023 11:51 PM
Try code below
var gr = new GlideRecord('incident');
gr.addActiveQuery();
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
if (gr.next()) {
gs.log('Most recent Incident Created Date = ' + gr.sys_created_on.getDisplayValue());
}
If you remove the .getDisplayValue() , then it will return the result in instance/time.