How to Subtract two dates in salary Component Formula Column

Hi,
I am trying to subtract the two dates: End Date and Start date on the salary slip to get the number of days in the month (not working days) in a salary component formula.

But I can’t seem to be able to subtract the two dates here.
The following don’t work:

  1. end_date - start_date - Error: Expected int got str
  2. getDate(end_date) - getDate(start_date) - Error: getDate is not defined.
  3. frappe.datetime.get_day_diff(begin, end) - this works in other places but not on the salary component formula space. Error: frappe is not defined.

Can’t seem to be able to use generic JS or Python functions.

Any help is welcome!

Thanks!

I managed to achieve a very situational workaround using the string function in python.
int(end_date[8:10]) - int(start_date[8:10]) + 1

But any help on the original is very welcome.

I’m trying to figure out how dates work in a salary component formula as well. I’m going to did through the code to figure out how it works. What I did find so far, is where the formula is evaluated. Maybe this will help someone in the future.

Here’s some more details. Looks like the safe_eval function is being used, which whitelists specific global variables. This will at least help us know what functions are available to be called.

https://github.com/frappe/frappe/blob/5e96e92a550bf23581f49a78a1ca2b2b139e4da8/frappe/init.py#L2239

I wish I knew how the date was formatted. Seems to be a string, but I’m not sure how it’s formatted. If I know, I could probably convert it to a unix timestamp. Then it would be easy to work with in this very restricted safe_eval.

I think I figured it out though. The following two whitelists are what is available in the formula.

https://github.com/frappe/frappe/blob/5e96e92a550bf23581f49a78a1ca2b2b139e4da8/frappe/init.py#L2241

Notice, date and getdate are both available. This probably is flexible enough to work with the date object or convert it to a unix timestamp and do raw math.

I have confirmed that this works. If you’re trying to use dates with a salary component formula, use the getdate() function to convert the date into a python date object. Then you can go from there.

For example, if I want to get the month when an employee was hired, I would use the following.

getdate(date_of_joining).month

You can see all the available attributes on a python date object at the following docs.