user macro to format date

Hi all,

I would like to make a user macro, which does a similar thing to the date formatting used with tasks.

User macro with the bodytext (or a parameter) as a date (dd.mm.yyyy).

If the date is in the past, the formatting shall be changed to red.

Is this possible with a simple user macro?

Thx for any help.

Reto E.

1 answer

1 accepted

0 vote
Davin Studer Community Champion Jun 30, 2014

This is super hacky but it works. Maybe there is a better way to handle dates with velocity, but I've had a devil of a time finding it.

## Developed by: Davin Studer
## Date created: 7/1/2014

## @param Date:title=Date|type=date|desc=Date to start from. Format: dd/mm/YYYY

#set ($len = 0)
#set ($i = 0)
#set ($parts = $paramDate.split("/"))
#set ($today = $action.dateFormatter.getCalendar())

## Having trouble finding out the length/size of $parts ... brute force it
#foreach ($part in $parts)
    #set ($len = $len + 1)
#end

#if ($len == 3)
    ## Buiding date variable from parameter string ... can't seem to find a better way to do it with velocity
    #foreach ($part in $parts)
        #if ($i == 0)
            #set ($day = $generalUtil.convertToInteger("$part"))
        #elseif ($i == 1)
            #set ($month = $generalUtil.convertToInteger("$part"))
            #set ($month = $month - 1)
        #else
            #set ($year = $generalUtil.convertToInteger("$part"))
        #end

        #set ($i = $i + 1)
    #end

    #set ($cal = $action.dateFormatter.getCalendar())
    $cal.set($year, $month, $day, 0, 0, 0)

    ## Compare enetered date to today
    #if($today.timeInMillis > $cal.timeInMillis)
        <span style="color: #FF0000">$paramDate</span>
    #else
        $paramDate
    #end
#else
    bad date
#end

Thx Devin.

I'm new to velocity in confluence. And couldn't find a way to convert a string into a date to work with. Espacially because confluence has a param-type date, but it is treated as a normal string ...

Your 'hack' works for now.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

373 views 20 10
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