GlideDateTime.before() or .after() not working, please help?

joshuamayes
Giga Expert

Short Version:

https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=c_APIRef.dita gives the following example for using GlideDateTime.before()

var gdt1 = new GlideDateTime("2016-05-09 10:11:12");

var gdt2 = new GlideDateTime("2017-06-12 15:11:12");

gs.info(gdt1.before(gdt2));

However when putting it into a background script it outputs "undefined".   Why?

Long Version:

I am trying to build a transform script that treats records differently depending on if a field has a date (stored as string) before 2015-01-01.   My idea was to just use GlideDateTime.before() to check if the log_date was before the cutoff date.   For example:

case 'OPEN'   : //This status + assigned to qrsinv is default method for creating a new asset.   Anything with this status before Cutoff date 01/01/2015 - Mark as lost

        var splitDate = source.u_log_date.split("/");

        var strDate = splitDate[2]+'-'+splitDate[0]+'-'+splitDate[1] + ' 00:00:00';

        var gd = new GlideDateTime(strDate);

        var cutoff = new GlideDateTime('2015-01-01 00:00:00');

if(gd.before(cutoff)){ //Do my code

}else{// more code

}

break;

I'm starting to think that this isn't viable.   Any ideas?


					
				
			
			
				
			
			
				
1 ACCEPTED SOLUTION

Yeah I just ran it in Helsinki and I am getting undefined too. The developer doc you are referring to is Istanbul. Here is for Helsinki:


https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=c_APIRef.dita



And before is not defined here, compareTo as mentioned in this Helsinki doc.


View solution in original post

5 REPLIES 5

shruti_tyagi
ServiceNow Employee
ServiceNow Employee

I ran this script in my OOB instance :


var gdt1 = new GlideDateTime("2016-05-09 10:11:12");


var gdt2 = new GlideDateTime("2017-06-12 15:11:12");


gs.info(gdt1.before(gdt2));



It gave me correct output: *** Script: true



Not sure why not working for you, on which version you are on?


Interesting...


I'm on Helsinki.   Sorry I didn't think to include that in my post.



Also worth noting that I was testing that snippet in background scripts.   Did you run it in some other module?


Yeah I just ran it in Helsinki and I am getting undefined too. The developer doc you are referring to is Istanbul. Here is for Helsinki:


https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=c_APIRef.dita



And before is not defined here, compareTo as mentioned in this Helsinki doc.


Thank you so much!! I was going bald trying to figure this out.