How to get time difference between 2 dates in client script

codedude
Mega Expert

I have 2 datetime fields and also have a duration (single line of text). I am trying to compute the difference between the start and end date and put the difference in the duration field.

I am having a difficult time figuring this out via client script, this is on a service catalog item. I tried to use the "gs" api, but it frowns upon that in client scripts. I then tried this and still no luck:

var startDate = g_form.getValue("start_date");

var endDate = g_form.getValue("end_date");

alert(startDate + " ," + endDate);

     

var dc = new DurationCalculator();

var dur = dc.calcScheduleDuration(startDate, endDate);

alert(dur);

Any advice?

1 ACCEPTED SOLUTION

Abhinandan Pati
Giga Guru

Hi Josh



Methods of duration calculator can be used only on the server side. Make an Ajax call and use this method in script include.



You can also use GlideSchedule for duration calculation.



Thanks


Abhinandan


View solution in original post

7 REPLIES 7

Community Alums
Not applicable

What are your alerts returning? Try something like this:



var sd = g_form.getValue("start_date");


var ed = g_form.getValue("end_date");


var startDate = new GlideDateTime(sd);


var endDate = new GlideDateTime(ed);


var dif = gs.dateDiff(startDate, endDate, false);


alert(dif);




Cheers,



Tim


I am unable to use "gs" in client scripts..... I get this error :



The object "gs" should not be used in client scripts.


Community Alums
Not applicable

Ah, right, sorry...


Are there any other options?