---
title: "Calculate the duration between a date field's current and previous value"
canonical: "https://support.appfire.com/space/JMCF/465633819/Calculate%20the%20duration%20between%20a%20date%20field's%20current%20and%20previous%20value"
format: markdown
---
> Macro (aura-html)

### Abstract

<span style="color: #333333">This code snippet fetches the duration between a date field's current and previous value.</span>

### Logic

- <span style="color: #333333">Check for null values.</span>
- <span style="color: #333333">Fetch the field history of the date field.</span>
- <span style="color: #333333">Return the difference between the two dates in Long.</span>

### Snippet

```groovy
import java.text.SimpleDateFormat;
 
Date curDate = issue.get("<Date field>");
if (curDate == null)
  return null;
 
if (issue.getFieldHistory("<Date field>").size() == 0)
    return null;
oldVal = issue.getFieldHistory("<Date field>")[-1].getFrom()
 
if (oldVal==null)
 return null;
 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date oldDate = sdf.parse(oldVal);
return (curDate.getTime() - oldeDate.getTime())/1000L;
```

### Placeholders

| **Placeholder** | **Description** | **Example** |
| --- | --- | --- |
| `<Date field>` | Date in context | Due date |

### Examples

<span style="color: #333333">The output of the code snippet is a duration which you could use in a Groovy script, for example, to calculate and display the duration between the current Due date and the last Due date.</span>

```groovy
import java.text.SimpleDateFormat;
 
Date curDate = issue.get("duedate");
if (curDate == null)
  return null;
 
if (issue.getFieldHistory("duedate").size() == 0)
    return null;
oldVal = issue.getFieldHistory("duedate")[-1].getFrom()
 
if (oldVal==null)
 return null;
 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date oldDate = sdf.parse(oldVal);
return (curDate.getTime() - oldDate.getTime())/1000L;
```

### References

- [issue variable](https://appfire.atlassian.net/wiki/spaces/JMCF/pages/465732169)
- [Accessing the fields of an issue](https://appfire.atlassian.net/wiki/spaces/JMCF/pages/465371580)
- [**Groovy Documentation**](http://groovy.codehaus.org/User+Guide)