Date calculation

sainath3
Mega Guru

Hi All,

 

I have a requirement below:

 

Invoice billing amount : 300: start date: 01-11-2024, end date: 19-11-2024.

 

so here requirement is to calculate Invoice billing amount should calculate only for the days between start & end date are: 19/30*300=190.

 

but in script it is calculating only 18 days.

                    var startDate = new GlideDateTime(invoice.u_start_date);
                    var endDate = new GlideDateTime(invoice.u_end_date);
                    var diff = GlideDateTime.subtract(startDate, endDate);
                    var days = diff.getRoundedDayPart();
                    gs.info("days "+days);// Here populating 18 days
 
why it is not considering the start date/end date, can anyone tell me how to show it as 19 days.
But I don't want to encourage add 1 day in the end by hardcode.
                 
1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi @sainath3,

 

Whilst I salute the resistance to 'manually' add the additional day, the subtract method in both the GlideDate and GlideDateTime API will not factor in include the start date for example into the calculation.

 

Can you expand on the values you retrieve in 'invoice.u_start_date' fields? Are they dates or date time values?

I notice you're leveraging the 'getRoundedDayPart()' method which will round up if you are in fact handling date time values and the hours diff is over 12 hours (even 1 second over the 12 hours diff. IF it is exactly 12 hours, it will not round up).

 

Depending on whether you are handling date time values or just dates, you could simply ensure you only use the date value and use the GlideDate API to ensure hours are not taken into consideration and then always add one day.

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.





Thanks, Robbie

View solution in original post

3 REPLIES 3

Robbie
Kilo Patron
Kilo Patron

Hi @sainath3,

 

Whilst I salute the resistance to 'manually' add the additional day, the subtract method in both the GlideDate and GlideDateTime API will not factor in include the start date for example into the calculation.

 

Can you expand on the values you retrieve in 'invoice.u_start_date' fields? Are they dates or date time values?

I notice you're leveraging the 'getRoundedDayPart()' method which will round up if you are in fact handling date time values and the hours diff is over 12 hours (even 1 second over the 12 hours diff. IF it is exactly 12 hours, it will not round up).

 

Depending on whether you are handling date time values or just dates, you could simply ensure you only use the date value and use the GlideDate API to ensure hours are not taken into consideration and then always add one day.

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.





Thanks, Robbie

Hi Robbie,

 

Thanks for quick reply.

 

we are considering only dates not date & time.

even after removing "getRoundedDayPart()" method also getting same no of days.

 

 

Hi @sainath3,

 

Thanks for confirming.

Just to be clear (as mentioned above), the the subtract method in both the GlideDate (and GlideDateTime) API is simply the difference and will not factor in or include the start date for example into the calculation.

Therefore you will need to plus 1. The reason why I asked if you were handling date objects or datetime objects was for the rounding aspect, but as thi is not required you can rest assured that by adding 1 will always be correct.

 

So - to simply answer your question. The subtract method is correct and working as designed. If you want to include the startdate into the calculate you need to plus 1.

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.





Thanks, Robbie