How to evaluate values on Confluence Page?
Asked by Troy S:

I what to report the number of days until a due date. The pseudo code might look like:

```

```

```
June 15, 2012 - NOW() = 63 days
```

```

```

Can a Confluence Webpage generate the value dynamically when before it is presented to the user to let them know the number of days before the due date?

I hope that make sense.

Thanks,

Troy


Answers:
author: Remo Siegwart
body: You could write a user macro for that. Here is my example that counts down the number of days until a specific date:

```
## Macro title: Date Countdown## Macro has a body: No## Body processing: No macro body#### Developed by: Remo Siegwart## Date created: 14/04/2012## This user macro counts down the number of days until a specific date## @param endDate:title=End Date|type=date|required=true|desc=The end date. Format: mm/dd/YYYY## declare variables and initialize as java date objects with current date#set ( $endDate = $content.currentDate )#set ( $currentDate = $content.currentDate )## parse endDate param to milliseconds and set as time of endDate variable$endDate.setTime($content.currentDate.parse($paramendDate))## now we have 2 valid java date objects to work with#if($currentDate.before($endDate))## currentDate is before endDate =&gt; calculate deltas#set( $delta =  ($endDate.getTime() - $currentDate.getTime()) / 1000)## calculate remaining seconds#set( $deltaSeconds = $delta % 60 )#set( $delta =  $delta / 60 )## calculate remaining minutes#set( $deltaMinutes = $delta % 60 )#set( $delta =  $delta / 60 )## calculate remaining hours#set( $deltaHours = $delta % 24 )#set( $delta =  $delta / 24 )## calculate remaining days#set( $deltaDays = $delta % 365 )&lt;div class="aui-message info shadowed"&gt;  &lt;p&gt;Countdown until &lt;strong&gt;$action.dateFormatter.format($endDate): $deltaDays days $deltaHours hours $deltaMinutes minutes $deltaSeconds seconds&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt;#else## currentDate is after endDate&lt;div class="aui-message success shadowed"&gt;  &lt;p&gt;Countdown to &lt;strong&gt;$action.dateFormatter.format($endDate)&lt;/strong&gt; expired &lt;strong&gt;$generalUtil.getRelativeTime($endDate)&lt;/strong&gt;!&lt;/p&gt;&lt;/div&gt;#end
```

Learn more about user macros here: [Guide To User Macro Templates](http://confluence.atlassian.com/x/UolYDQ)

Hope this helps

comments: author: Troy S
body: Thanks for your help Remo.

Is this feature available in Confluence 3.5?

Do I need special admin privileges to create and install a user macro?

I can't find where I would install the user macro.

Thanks,


author: Remo Siegwart
body: Yes, user macros are available in Confluence 3.5, but you need to have [System Administrator](http://confluence.atlassian.com/display/DOC/Global+Permissions+Overview) permissions in order to create user macros.

Source: [Writing User Macros](http://confluence.atlassian.com/x/hRE)


author: Reatha Ek
body: Hi!

I am looking for a way to manage document retention periods. Imho this could be used just to do that, provided that every document has a clearly set end-date?

Could that be extended in a way that documents that are beyond the end date will be archived and/or purged?

Kind regards

Bernd


author: Remo Siegwart
body: User macros are only executed on page load. I would suggest you create a custom plugin with a [Job Module](https://developer.atlassian.com/display/CONFDEV/Job+Module) for that.


author: Tim1979
body: hi,  

thank you for this macro!  

but the macro does not calculate the correct number of days.  

<br />

30.12.2012 (US 12/30/2012)

and

30.12.2013 (US 12/30/2013)

it comes out the same result?  

why?  

thank you for your help


author: Remo Siegwart
body: Actually, it does calculate the correct number of days, just the remaining years are not displayed yet. If you want to display the remaining years also as days, you could rewrite the macro like this:

```
## Macro title: Date Countdown## Macro has a body: No## Body processing: No macro body#### Developed by: Remo Siegwart## Date created: 21/07/2012## This user macro counts down the number of days until a specific date## @param endDate:title=End Date|type=date|required=true|desc=The end date. Format: mm/dd/YYYY## declare variables and initialize as java date objects with current date#set ( $endDate = $content.currentDate )#set ( $currentDate = $content.currentDate )## parse endDate param to milliseconds and set as time of endDate variable$endDate.setTime($content.currentDate.parse($paramendDate))## now we have 2 valid java date objects to work with#if($currentDate.before($endDate))## currentDate is before endDate =&gt; calculate delta#set( $delta =  ($endDate.getTime() - $currentDate.getTime()))## calculate remaining days#set( $deltaDays = ($delta / (24 * 3600 * 1000)) )## calculate remaining hours#set( $deltaHours = ($delta % (24 * 3600 * 1000)) / (3600 * 1000) )## calculate remaining minutes#set( $deltaMinutes = (($delta % (24 * 3600 * 1000)) % (3600 * 1000) / (60 * 1000)) )## calculate remaining seconds#set( $deltaSeconds = (($delta % (24 * 3600 * 1000)) % (3600 * 1000) % (60 * 1000)) / (1000) )&lt;div class="aui-message info shadowed"&gt;  &lt;p&gt;Countdown until &lt;strong&gt;$action.dateFormatter.format($endDate): $deltaDays days $deltaHours hours $deltaMinutes minutes $deltaSeconds seconds&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt;#else## currentDate is after endDate&lt;div class="aui-message success shadowed"&gt;  &lt;p&gt;Countdown to &lt;strong&gt;$action.dateFormatter.format($endDate)&lt;/strong&gt; expired &lt;strong&gt;$generalUtil.getRelativeTime($endDate)&lt;/strong&gt;!&lt;/p&gt;&lt;/div&gt;#end
```


author: Tim1979
body: Thank you for this excellent support. It works


author: Avinash
body: Hi Remo,

How will the velocity code be if i have to display only the current date?


author: Remo Siegwart
body: Try something like this:$action.dateFormatter.format($content.currentDate)


author: Avinash
body: Works great! Thanks! :)


author: Melissa Freeman
body: I'm trying to get the message to say something like "N days, N hours, N minutes, N seconds until \<event\>" where \<event\> is the name of an event that you specify in the macro, for example, Bob's Anniversary.

Any idea how I can make it print a customizable message like this instead of what is currently prints?

Thanks!


author: CelesteCS
body: Hello,

For simple calculations have a look at CelesteCS Math for Confluence plugin: <https://marketplace.atlassian.com/plugins/com.celestecs.confluence.celestecs-math>. Macros as well as table cell values may be used as formula operands.

----- UPDATE -----

We have added Date type support to Math macro so from now you may use our macro to accomplish what you want. Just specify the EXCEL-style formula, like "DAYS(NOW(), DATE(2016, 04,17))".

Thanks!


author: Roger Henty
body: Hello,

I'm trying to setup a user macro that inserts a current timestamp on new entries as they're added to a page.

I'm able to show the current time/date using the below, but I'm having trouble figuring out how to get the value to remain static, rather than updating to show the current time:

## @noparams  

$action.dateFormatter.formatDateTime($content.currentDate)

I wondered whether anyone would have suggestions on what I'd need to add to get this to act as an actual timestamp rather than a clock?

Thanks!


author: Tim1979
body: hi,  
thank you for this macro!  

but the macro does not calculate the correct number of days.  

30.12.2012 (US 12/30/2012)

and

30.12.2013 (US 12/30/2013)

it comes out the same result?  

why?  

thank you for your help


Tags: dynamic

user-macro