On change client script to calculate the difference in start date and end date and display the result in another field no_of_days in days

test1231998
Tera Contributor

I have two fields start date and end date for my table.I wanted to write an onchange client script such that upon selection of both dates the difference in dates should be auto populated in no-of_days field which is of type integer.

For eg:If start date is 09-09-2020 and end date is 11-09-2020 The no_of_days field should have 3.

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

@deepika 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate answer as correct & helpful to close the thread.

If not, please let us know if you need some more assistance.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate answer as correct & helpful to close the thread.

If not, please let us know if you need some more assistance.

Regards
Ankur

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Deepika,

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate answer as correct & helpful to close the thread.

If not, please let us know if you need some more assistance.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

The SN Nerd
Giga Sage
Giga Sage

You can do something similar with only one line of code using functional fields

glidefunction:datediff(opened, resolved_at)

datediff()

Takes 2 date/time fields as input, calculates the difference between the dates in days, minutes, and seconds, and returns the results as a duration field value.

https://docs.servicenow.com/bundle/paris-platform-administration/page/build/platform-functions/concept/platform-support-functions.html


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

I know this is an old thread, but you just saved me hours of work. Thank you for the heads up on Function fields!