Groovy formula - date format
Asked by Shiva Jamei:

I am using this formula for a calculated custom field (duration date calculation).

I need the number of "days" that has passed since a date. In my case the custom field "Race Handover" is a date that we insert manually, and I need to see each day, how many days have passes since the handover date.

Here is what I put in the formula:

secondsBetween(issue.get("RACE Handover"), new Date(), "days" )

But what I am getting in Jira is the duration in Weeks format

so it shows: "26 weeks"

instead I would like to get: 182 days.

I wonder if you can help me understand how to change the format of the duration from weeks to days.

Thanks.


Answers:
author: David Fischer
body: Hi [@Shiva Jamei](/t5/user/viewprofilepage/user-id/5043062)

unfortunately, you cannot control the formatting of Calculated *Duration* fields. If you always want to display the duration in weeks and weeks only, whether the value is 1s (-\> 0 weeks) or 10 years (-\> 520 weeks), then you should create a calculated *Number* field and have it return the number of weeks between the two dates:

```
secondsBetween(issue.get("RACE Handover"), new Date(), "weeks" ) / 3600 / 24 / 7
```

and then use the formatting option to display:

```
$value weeks?
```

comments: author: Shiva Jamei
body: Thanks [@David Fischer](/t5/user/viewprofilepage/user-id/538255) I have created the calculated number field

This is the formula that I put:

secondsBetween(issue.get("RACE Handover"), new Date(), "weeks" ) / 3600 / 24 / 7 \* 7 and here is the output:

In RACE since: \[The old field to calculate the duration\]

**23 weeks, 2 days**

RACE duration days: \[The new field \]

**161**

So it is getting the 23 weeks and showing 23\*7 as 161 days, but it is not adding the "2days", so I expect to see 163 days instead of 161.


author: David Fischer
body: That's because, at the end, you divide by 7 and then multiply by 7. That rounds up the value to whole weeks. Just remove /7\*7 at the end.


author: Shiva Jamei
body: This is what I get then by this:

secondsBetween(issue.get("RACE Handover"), new Date(), "weeks" ) / 3600 /24

In RACE since: 19 weeks, 1 day

RACE duration days: No such property: weeks for class: script16668541552001129910092


author: David Fischer
body: Are you sure you didn't remove the double quotes around *weeks* ?


author: Shiva Jamei
body: ![groovy.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/222869i5B5957A2C0040C1E/image-size/large?v=v2&px=999 "groovy.jpg")


author: David Fischer
body: It's the format expression that's incorrect, it should be a valid groovy expression. Try this:

```
"$value weeks"
```


author: Shiva Jamei
body: Thanks, this is becoming interesting, I change the format, and now I get: 133.0 weeks

![groovy 1.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/222872i062CA797B599D5B5/image-size/large?v=v2&px=999 "groovy 1.jpg")


author: David Fischer
body: If you need weeks, the value should return a number of weeks, so you should add /7 to the formula.

As for not displaying the ".0", in the format expression, try this:

```
"${(int)value} weeks"
```


author: Shiva Jamei
body: Thanks [@David Fischer](/t5/user/viewprofilepage/user-id/538255)

It is fixed now, I did not wanted it in weeks as the original field is already in weeks and I wanted the new calculation in Days, so I just replaced weeks in days and now I get the correct number! ![groovy 2.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/222881i75D1EFEC500DA383/image-size/large?v=v2&px=999 "groovy 2.jpg")

which is

![groovy 3.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/222882iCC149DC6A61ED326/image-size/large?v=v2&px=999 "groovy 3.jpg")

Thanks a lot!


author: Paloma Fondon Araujo
body: Hi [@Shiva Jamei](/t5/user/viewprofilepage/user-id/5043062)

[Here](https://library.adaptavist.com/entity/calculate-the-difference-between-two-dates) is an example of a script from the adaptavist library:

Or If you have automation for jira, you can create the following rule. You could include a trigger that runs every day at 9:00 am (for example) and enter a JQL that contains all issues that need to be updated. (or the trigger you already have included in the rule)

To calculate how many days have passed since the Handover Date you can include the following formula where *customfield_XXXXX* is your Handover Date field:

```
{{customfield_XXXXX.diff(now).days}}
```

![Days from Date to now.png](https://community.atlassian.com/t5/image/serverpage/image-id/219758i8EA83E154F437D49/image-size/large?v=v2&px=999 "Days from Date to now.png")  
I hope it helps,  
Regards

comments: author: Shiva Jamei
body: Thanks Paloma,

I have tried the adaptivist formula, but it didn't work, I don't know if it is compatible with "JMCF" scripts.

This is the custom field that I am actually using now and in the groovy script I put "secondsBetween(issue.get("RACE Handover"), new Date(), "days" )"

![jira.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/220056i1235A1A2B6436DFB/image-size/large?v=v2&px=999 "jira.jpg")


Tags: duration

groovy

server