Jump to content
  • 0

DATEDIFF: calculate Years, months and days at same time


roattw

Question

In excel if I wanted to know the number of Year and months and days until something I would use:

=DATEDIF(B2,TODAY(),"y") & " Years, " & DATEDIF(B2,TODAY(),"ym") & " Months, " & DATEDIF(B2,TODAY(),"md") & " Days"

In CB calculated fields I can get one of the intervals - year OR month OR day but not all three using:

Datediff(day,GetUTCDate(),[@field:ExpirationDate])

Can I convert that excel datediff to a CB friendly alrternative?

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

You are amazing, thank you.  I had it close.  But it turns out this isnt quite what I intended after I saw it live.  This does in fact give the Year, Months, Days date difference between today and the expiration date in the independent forms of:

1 year until expiration, OR
12 months until expiration, OR
342 days until expiration.

After looking at it I realized what Im hoping is for is closer to the Excel version that refreshes and does a combined summary of years, months, days until expiration like examples:  

Item A expires in: 1 yr, 2 mos and 14 days

Item B expires in: 0 yr, 8 mos, 6 days.

Different animal I suspect.

Link to comment
Share on other sites

  • 0

This is closer to what you are looking for:

Convert(varchar(10),DATEDIFF(yy, [@field:ExpirationDate], GetUTCDate()) )+ 'year' + Convert(varchar(10),DATEDIFF(mm, [@field:ExpirationDate], GetUTCDate()) ) + 'month'  + Convert(varchar(10),ABS(DATEDIFF(mm, DATEADD(yy, Datediff(year,[@field:ExpirationDate], GetUTCDate()), [@field:ExpirationDate]),  [@field:ExpirationDate]))) + 'day'

 

Link to comment
Share on other sites

  • 0

Always appreciate your help!  Getting closer but not quite.  That formula is giving me a miscount for months and days still. It renders these dates this way:

GetUTCdate date                                Expiration Date:
2/16/18                                                     5/6/2020                                       2 years   27 months   24 days    (Should be   2 yrs, 2 months, 20  days)

 

 

Link to comment
Share on other sites

  • 0

Hi @roattw

I have the same workflow but I somehow found a workaround for this.

 

I created 3 Virtual fields, one for Year calculation, one for month, and another one for year. 

Inside the virtual fields, here are my formula.

For Year:

Datediff(mm, [@field:Birthdate], GetUTCDate())/12

For Month:

Datediff(day, [@field:Birthdate], GetUTCDate())/30 - ([@cbParamVirtual1]*12)

For Day:

Datediff(day, [@field:Birthdate], GetUTCDate()) - ([@cbParamVirtual1]*365) - ([@cbParamVirtual2]*30)

 

And then I concatenated them all to the field that is on my table:

CONVERT(VARCHAR, [@cbParamVirtual1]) + "YEARS " + 
CONVERT(VARCHAR, [@cbParamVirtual2]) + "MONTHS " + 
CONVERT(VARCHAR,[@cbParamVirtual3]) + "DAYS"

 

This was my output.

Screenshot_2.png.ff8ca42d7fbfa6a71fe62fa442c0137a.png

 

I also hid the Virtual fields using HTML Block so it won't show on the DataPage.

 

Hope this helps!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...