Appian Community
Site
Search
Sign In/Register
Site
Search
User
DISCUSS
LEARN
SUCCESS
SUPPORT
Documentation
AppMarket
More
Cancel
I'm looking for ...
State
Not Answered
Replies
12 replies
Subscribers
8 subscribers
Views
5019 views
Users
0 members are here
Share
More
Cancel
Related Discussions
Home
»
Discussions
»
AI and Rules
Hi All, Currently I am working on a requirement where we need to comp
souravs
Certified Lead Developer
over 9 years ago
Hi All,
Currently I am working on a requirement where we need to compute the number of months based on start date and end date. Suppose say for example start date is 25/12/2015 and end date is 31/03/2017. The total number of months should be calculated as 15 months including the date precision. How to achieve this functionality?
OriginalPostID-183629
OriginalPostID-183629
Discussion posts and replies are publicly visible
0
shivakanthr
over 9 years ago
Use Calworkdays() function. It returns number of days between two dates. then convert days to months.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
janakik
over 9 years ago
I don't think calWorkDays() function will work for his requirement.
May be you can make use of below expression
tointeger((ri!endDate-ri!startDate)/ri!days)
Where ri!days is average number of days for a month - 30
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
shivakanthr
over 9 years ago
Yup It will calculate only work days
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
souravs
Certified Lead Developer
over 9 years ago
Thanks Janaki But if I try with these example with end date 31-Mar-2017 and start date 10-Dec-2015. It calculates to 15.9 which is not accurate.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
vaibhavm
A Score Level 2
over 9 years ago
You can try this trunc(yearfrac(ri!startdate,ri!enddate,3)*12)&" months "
&round((
yearfrac(ri!startdate,ri!enddate,3)*12-trunc(yearfrac(ri!startdate,ri!enddate,3)*12))*30,0)&" days"....gives you the most accurate months and days
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
vaibhavm
A Score Level 2
over 9 years ago
There are certain exceptions with this expression because I have assumed the number of days as 30
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Rahul Gundu
over 9 years ago
Try the below code.
with(
local!years:year(ri!todate)-year(ri!fromDate),
local!months:(local!years)*12+(month(ri!todate)-month(ri!fromdate))-1,
local!days:(daysinmonth(month(ri!fromdate),year(ri!fromDate))-day(ri!fromDate))+day(ri!todate),
local!months&" months, "&local!days&" days"
)
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
sikhivahans
over 9 years ago
@souravs I would like to suggest you to follow a precise approach rather than depending on things such as calculating based on the days in between etc. Further I would like to suggest you to go through the attached code snippet once as I believe that it's the actual way of doing and doing so will also lead to accurate results as well as allows to control the inclusive/exclusive behavior if needed.
Let's see if there are any other better suggestions from the community.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
sikhivahans
over 9 years ago
@souravs If you haven't made a search in the forum earlier, I would like to suggest to go through the post at
forum.appian.com/.../e-144093
in which @philb has elegantly written a rule and it could give lot more ideas if explored.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Rahul Gundu
over 9 years ago
The above code, which i was shared, fails in some scenarios, Check with the below code. I feel it will fulfill your requirement
with(
local!years:year(ri!todate)-year(ri!fromDate),
local!months:(local!years)*12+(month(ri!todate)-month(ri!fromdate))-1,
local!days:(daysinmonth(month(ri!fromdate),year(ri!fromDate))-day(ri!fromDate))+day(ri!todate),
if(
local!days>=fn!daysinmonth(month(ri!todate),year(ri!toDate)),
local!months+1&" months, "&local!days-fn!daysinmonth(month(ri!todate),year(ri!toDate))&" days",
local!months&" months, "&local!days&" days"
)
)
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
>