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

Shruti
Mega Sage
Mega Sage

    var current_date = new GlideDateTime();  // current date
    var end_date = '2024-10-31 05:11:50'; // planned end date
    var new_date = new GlideDateTime(end_date);
    var difference = GlideDateTime.subtract(new_date,current_date);
    var differenceInDays = difference.getNumericValue() / 86400000;
    gs.info(parseInt(differenceInDays));
    

Aryan
Tera Contributor

Hi Shruti,

 

Thank you for the quick reply, i will try this and let you know.

Aryan
Tera Contributor

Hi @Shruti  ,

 

You code is working and display the correct data, but it  display the data in decimal can we remove the decimal for example can we remove everything and display on 20 days.

 

Aryan_0-1730882433398.png

 

Hi , I have used parseInt in last line to remove decimal part. Can you try that

parseInt(differenceInDays)