Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to evaluate values on Confluence Page?

Troy S April 13, 2012

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

4 answers

1 accepted

2 votes
Answer accepted
Remo Siegwart
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 13, 2012

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 => 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 )

<div class="aui-message info shadowed">
  <p>Countdown until <strong>$action.dateFormatter.format($endDate): $deltaDays days $deltaHours hours $deltaMinutes minutes $deltaSeconds seconds</strong></p>
</div>

#else

## currentDate is after endDate
<div class="aui-message success shadowed">
  <p>Countdown to <strong>$action.dateFormatter.format($endDate)</strong> expired <strong>$generalUtil.getRelativeTime($endDate)</strong>!</p>
</div>

#end

Learn more about user macros here: Guide To User Macro Templates

Hope this helps

Troy S April 15, 2012

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,

Remo Siegwart
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2012

Yes, user macros are available in Confluence 3.5, but you need to have System Administrator permissions in order to create user macros.

Source: Writing User Macros

Reatha Ek June 12, 2012

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

Remo Siegwart
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 12, 2012

User macros are only executed on page load. I would suggest you create a custom plugin with a Job Module for that.

Tim1979 July 19, 2012

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

Remo Siegwart
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 20, 2012

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 => 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) )

<div class="aui-message info shadowed">
  <p>Countdown until <strong>$action.dateFormatter.format($endDate): $deltaDays days $deltaHours hours $deltaMinutes minutes $deltaSeconds seconds</strong></p>
</div>

#else

## currentDate is after endDate
<div class="aui-message success shadowed">
  <p>Countdown to <strong>$action.dateFormatter.format($endDate)</strong> expired <strong>$generalUtil.getRelativeTime($endDate)</strong>!</p>
</div>

#end
Like Murali likes this
Tim1979 July 22, 2012

Thank you for this excellent support. It works

Avinash September 12, 2012

Hi Remo,

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

Remo Siegwart
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 13, 2012
Try something like this: $action.dateFormatter.format($content.currentDate)
Avinash September 13, 2012

Works great! Thanks! :)

Melissa Freeman June 9, 2013

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!

0 votes
CelesteCS April 14, 2015

Hello,

For simple calculations have a look at CelesteCS Math for Confluence plugin: https://marketplace.atlassian.com/plugins/com.celestecs.confluence.celestecs-mathMacros 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!

0 votes
Roger Henty January 6, 2013

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!

0 votes
Tim1979 July 19, 2012

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events