How to calculate Days old based on created and planned end date

Aryan
Tera Contributor

Hi All,

 

We have an requirement where we have create a field call Days old and it will calculate Days old data based on below scenario.

 

Based on the daily scheduled job it  will take

 

  1. Based on the daily scheduled job it  will take current RUN date and time: This is when the report is created.
  2. Planned End date: This is the date something was supposed to be finished.

To find the Days Old:

  • Look at the Current RUN date and time.
  • Compare it to the Planned End date.
  • Count how many days have passed between these two dates.

So, Days Old shows how many days have passed since the planned end date up to the current date and time when the report is made.

 

Example:

  1. Current RUN date and time: November 6, 2024.
  2. Planned End date: October 31, 2024.

To calculate the Days Old:

  • Start with the Current RUN date: November 6, 2024.
  • Compare it to the Planned End date: October 31, 2024.
  • Count the days between these two dates.

From October 31 to November 6, there are 6 days.

So, the Days Old would be 6 days. This means it has been 6 days since the planned end date.

 

Can anyone help me on this?

1 ACCEPTED SOLUTION

   var currentTime = new GlideDateTime();
    var endDate = new GlideDateTime(gr.end_date);
    var difference = GlideDateTime.subtract(endDate,currentTime);
    var differenceInDays = difference.getNumericValue() / 86400000;
    gr.u_days_old = parseInt(differenceInDays);
    gr.update();

View solution in original post

8 REPLIES 8

Aryan
Tera Contributor
Below is my code under while loop:
 
can you review and let me know if any modification needed.
 
    var currentTime = new GlideDateTime();
    var endDate = new GlideDateTime(gr.end_date);
    var difference = GlideDateTime.subtract(endDate,currentTime);
    var differenceInDays = difference.getNumericValue() / 86400000;
    gs.info(parseInt(differenceInDays));
    gr.u_days_old = differenceInDays;
    gr.update();

   var currentTime = new GlideDateTime();
    var endDate = new GlideDateTime(gr.end_date);
    var difference = GlideDateTime.subtract(endDate,currentTime);
    var differenceInDays = difference.getNumericValue() / 86400000;
    gr.u_days_old = parseInt(differenceInDays);
    gr.update();

Aryan
Tera Contributor

Thanks @Shruti   this works.

Aryan
Tera Contributor

Thank you @Shruti