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

This post might be helpful:



Client Script Date/Time Functions



I've used some of these functions, in conjunction with GlideAjax, to calculate date/time and populate a field (CI). Never done it for a cat item, but I have used GlideAjax calls on a cat item before successfully.



Apologies, should have read your post more thoroughly, wouldn't have suggested the 'gs' thing!


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


Abhinandan Pati
Giga Guru

Hi Josh



Don't forget to set Timezone & Schedule before using these methods. And Date/Time format also matters.



Thanks


Abhinandan