Notifications

2862 views

gs.dateDiff() returns invalid results (Global GlideSystem)

 

Overview
Problems can occur when using gs.dateDiff() with GlideDateTime objects.
Procedure
The dateDiff() method expects parameters in the user/system format, and the GlideDateTime constructors expect parameters in the internal format (yyyy-MM-dd HH:mm:ss) and in the UTC time zone.

Using the dateDiff() method with other formats may return invalid results.

Instead, of using gs.dateDiff() use the GlideDateTime.subtract() method.

//correct

var gdt1 = GlideDateTime('2015-07-24 18:58:00');

var gdt2 = GlideDateTime('2015-07-24 19:01:00');

var duration2 = GlideDateTime.subtract(gdt1, gdt2);

 

//wrong

var gdt1 = GlideDateTime('2015-07-24 18:58:00');

var gdt2 = GlideDateTime('2015-07-24 19:01:00');

var duration1 = gs.dateDiff(gdt1, gdt2, false);

 

//if you must use gs.dateDiff(), do this

var gdt1 = GlideDateTime('2015-07-24 18:58:00');

var gdt2 = GlideDateTime('2015-07-24 19:01:00');

var duration1 = gs.dateDiff(gdt1.getDisplayValue(), gdt2.getDisplayValue(), false);

 

The gs.dateDiff() method is not available in scoped applications.

Article Information

Last Updated:2016-06-22 11:19:05
Published:2016-06-22