How to evaluate values on Confluence Page?

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

This widget could not be displayed.

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

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,

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

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

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

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

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

Thank you for this excellent support. It works

Hi Remo,

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

Try something like this: $action.dateFormatter.format($content.currentDate)

Works great! Thanks! :)

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!

This widget could not be displayed.

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

This widget could not be displayed.

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!

This widget could not be displayed.

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!

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Monday in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

460 views 6 6
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you