GlideDateTime Comparison

frauburgard
Kilo Explorer

I am working on a script include in which I pull an expiration date from the existing table and compare it to the current Date/Time.   I have written the following script:

var gdt_expdt = new GlideDateTime();

  gdt_expdt.setValue(gr_usr.getValue('expiration_date'));

  var gdt_updt = new GlideDateTime();

  gdt_updt.compareTo(gdt_expdt);

 

When I log the result, it shows that gdt_updt is still the current date/time.   Something seems to be going wrong in Line 4 where I have tried to use compareTo().   Can anyone see where it is that I am going wrong here?

1 ACCEPTED SOLUTION

ajitchandragiri
Giga Expert

your script worked for me. whats the value of gs.log(gdt_expdt), gs.log(gdt_updt) and gs.log(gdt_updt.compareTo(gdt_expdt))? My script



var gr_usr = new GlideRecord('incident');


gr_usr.addQuery("number", 'INC0047595');


gr_usr.query();


gr_usr.next();


gs.log('incident opened: ' + gr_usr.getValue('opened_at'));



var gdt_expdt = new GlideDateTime();


  gdt_expdt.setValue(gr_usr.getValue('opened_at'));


gs.log('created: ' + gdt_expdt);


  var gdt_updt = new GlideDateTime();


gs.log('current: ' + gdt_updt);


  gdt_updt.compareTo(gdt_expdt);


gs.log('compare: ' + gdt_updt.compareTo(gdt_expdt));



I dont think you need to worry about Timezone issue as script include runs on server. both current and expiration value are of server value.


View solution in original post

3 REPLIES 3

DrewW
Mega Sage
Mega Sage

Try


var gdt_expdt = gr_usr.expiration_date.getGlideObject()



And then see what you get.   I'm wondering if you are having a time zone issue.


ajitchandragiri
Giga Expert

your script worked for me. whats the value of gs.log(gdt_expdt), gs.log(gdt_updt) and gs.log(gdt_updt.compareTo(gdt_expdt))? My script



var gr_usr = new GlideRecord('incident');


gr_usr.addQuery("number", 'INC0047595');


gr_usr.query();


gr_usr.next();


gs.log('incident opened: ' + gr_usr.getValue('opened_at'));



var gdt_expdt = new GlideDateTime();


  gdt_expdt.setValue(gr_usr.getValue('opened_at'));


gs.log('created: ' + gdt_expdt);


  var gdt_updt = new GlideDateTime();


gs.log('current: ' + gdt_updt);


  gdt_updt.compareTo(gdt_expdt);


gs.log('compare: ' + gdt_updt.compareTo(gdt_expdt));



I dont think you need to worry about Timezone issue as script include runs on server. both current and expiration value are of server value.


It turns out my error was in how I was trying to log it.   Thanks for your help, Ajit!