Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Date value is returned as 1970

Khanna Ji
Tera Guru

Hi Friends,

 

I am trying to set the due date for a catalog item automatically based on the delivery time mentioned in the catalog item. But when I try to fetch the delivery time, it's giving me some random date.

 

KhannaJi_0-1670295226154.png

 

var grScCatItem = new GlideRecord('sc_cat_item');
if (grScCatItem.get('53988b4e874459106995d6c73cbb35d0')) {
    gs.info(grScCatItem.getValue('delivery_time'));
}

*** Script: 1970-01-08 02:00:00

 

How can I convert the delivery time to set the due date considering the schedule of 9*5?

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

Hi, durations are recorded in hours\minutes\seconds from Epoc IE 1970-01-01 00:00:00 and so this is correct\expected behaviour.

Based on your post I think your requirements can be delivered by

DurationCalculator | ServiceNow Developers

Using DurationCalculator to calculate a due date (servicenow.com)

View solution in original post

4 REPLIES 4

Saurabh Gupta
Kilo Patron

Hi,
If you want a date/time you should write a script include and call it in your script like below

var dateUtilForChangeMgmt = Class.create();
dateUtilForChangeMgmt.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getDateTime : function()
	{
		var startDate = new GlideDateTime(new GlideDateTime().getDisplayValue());// For overcoming the problem with daylight saving
		var dur1 = new GlideDuration(60 * 60 * 24 * 1000 * 2);
		var schedule_24_5=gs.getProperty('24*5_Schedule');//	Sys ID of "24*5" Schedule
		schedule.load(schedule_24_5); // 24*5 schedule
		var d1 = schedule.add(startDate, dur1);
		return d1;
	},
	
	type: 'dateUtilForChangeMgmt'



});

 

 

 

 

 

 


Thanks and Regards,

Saurabh Gupta

What is the schedule here? System says undefined

Tony Chatfield1
Kilo Patron

Hi, durations are recorded in hours\minutes\seconds from Epoc IE 1970-01-01 00:00:00 and so this is correct\expected behaviour.

Based on your post I think your requirements can be delivered by

DurationCalculator | ServiceNow Developers

Using DurationCalculator to calculate a due date (servicenow.com)

Hi Tony,

 

I tried but I still get don't get the required output.

 

var vdate;
var grStdChangeRecordProducer = new GlideRecord('std_change_record_producer');
if (grStdChangeRecordProducer.get('129ccf3c1b154c90e7d6202e6e4bcb75'))  {
    vdate = grStdChangeRecordProducer.getValue('delivery_time');
    gs.print('Delivery time:' + vdate)
    gs.print('Delivery time Display value:' + grStdChangeRecordProducer.getDisplayValue('delivery_time'));
}

var startDateTime = new GlideDateTime(vdate);
// Instantiate a new GlideDateTime object which has the end date as the current date and time
var endDateTime = new GlideDateTime(); 
var dur = new DurationCalculator();

// Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set
dur.setSchedule('58b1a872db30f740448880e2399619d2'); 
dur.calcScheduleDuration(startDateTime, endDateTime);
var secs = dur.getSeconds();
var totalSecs = dur.getTotalSeconds();

gs.print("SCHEDULE DURATION: SECS=" + secs + " TOTALSECS=" + totalSecs + " ENDTIME = " + endDateTime);


*** Script: Delivery time:1970-01-06 15:00:00
*** Script: Delivery time Display value:5 Days 15 Hours
*** Script: SCHEDULE DURATION: SECS=30154692.77 TOTALSECS=1669834092 ENDTIME = 2022-12-06 09:48:12