Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Today's date minus input number

Matthew Singleton January 31, 2018

So I have a macro that will subtract a number of days from today's date, is there a way of having an input that determines the numbers of days to be subtracted i.e Macro(5) would subtract 5 days from today, macro(12) would be todays date minus 12 days etc.

 

here is the hardcoded date marco:

#set ( $date = $action.dateFormatter.calendar )
$date.setTime($content.currentDate)
$date.add(6, -3)
<time datetime="$action.dateFormatter.formatGivenString('yyyy-MM-dd', $date.time)"/>

1 answer

1 accepted

1 vote
Answer accepted
Christo Mastoroudes [Adaptavist]
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.
January 31, 2018

Hi,

I assume you are using a user macro.

Yes, you will have to specify a macro parameter of type "int" and make it "required" so that the user must specify it.

Then you can reference it in your template code and make the appropriate calculation.

Screen Shot 2018-01-31 at 11.56.27.pngDocumentation: https://confluence.atlassian.com/doc/writing-user-macros-4485.html

 

If this answers your question please mark it as answered.

Matthew Singleton January 31, 2018

Thanks but it seems to not like a variable in $date.add as I get:

$date.add(6, $paramName) 31 Jan 2018

 

when using this:

## @param Name:title=Name|type=int|default=12|required=true

#set ( $date = $action.dateFormatter.calendar )
$date.setTime($content.currentDate)
$date.add(6, $paramName)
<time datetime="$action.dateFormatter.formatGivenString('yyyy-MM-dd', $date.time)"/>

Christo Mastoroudes [Adaptavist]
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.
January 31, 2018

Try just to print out the new variable directly on the page before you use it in a function to ensure that it has been declared correctly.

Matthew Singleton January 31, 2018

Yep that works fine! 

If I use 

...

$date.add(6, $paramName)
<time datetime="$action.dateFormatter.formatGivenString('yyyy-MM-dd', $date.time)"/>
$paramName

 

I get:

$date.add(6, $paramName) 31 Jan 2018 12

Matthew Singleton January 31, 2018

Got it working at last... looks like it sees the int as a string, so this converts it to an int:

 

## @param Name:title=Name|type=int|default=1
#set ( $Integer = 0 )

#set ($paramName = $Integer.parseInt($paramName))

#set ( $date = $action.dateFormatter.calendar )
$date.setTime($content.currentDate)
$date.add(6, $paramName)
<time datetime="$action.dateFormatter.formatGivenString('yyyy-MM-dd', $date.time)"/>

 

Found it here:

https://community.atlassian.com/t5/Answers-Developer-Questions/Confluence-User-macro-using-lt-less-than/qaq-p/568753

Christo Mastoroudes [Adaptavist]
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.
January 31, 2018

I think we found the same solution at about the same time....was just about to post it for you :)

 

Glad you got it working.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events