How to work with dates in user macro

Yaacov Weingarten
Contributor
June 4, 2018

Hi,

I am trying to write a user macro that checks the last modification of files. But when I try to compare the dates - nothing seems to work. So I wrote a small User macro to try and isolate the problem. (I thought I was following the advice located in the Community at: https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-parse-a-string-into-a-date-in-a-User-Macro/qaq-p/500908

and https://community.atlassian.com/t5/Confluence-questions/I-would-like-to-compare-dates/qaq-p/61312)

This is the code of the small macro (that just compares and prints out different formats of the creation and last modification times of the current page):

## This is an example macro
## @noparams

#set($mylist = "<br>")
#set( $createD = $content.getCreationDate())
#set( $modD = $content.getLastModificationDate())
#set( $createOb = $content.currentDate)
#set( $modOb = $content.currentDate)

#set ($mylist = $mylist + "file creation date (before parse): " + $createD + "<br>")
#set ($mylist = $mylist + "file modification date (before parse): " + $modD + "<br>")

#*
#set( $res = $createOb.setTime($content.currentDate.parse($createD)))
#set( $res = $modOb.setTime($content.currentDate.parse($modD)))
*#
$createOb.setTime($content.currentDate.parse($createD))
$modOb.setTime($content.currentDate.parse($modD))

#if ( $createOb.before($modOb))
    #set( $mylist = $mylist + "creation object time was before the modify object time <br>")
#elseif ( $createOb.after($modOb))
    #set( $mylist = $mylist + "creation object time was after the modify object time <br>")
#else
    #set( $mylist = $mylist + "creation object time was neither before or after the modify object time <br>")
#end

#set( $createDI = $createOb.getTime())
#set( $modDI = $modOb.getTime())
#set ($mylist = $mylist + "the unix time for creation is: " + $createDI + "<br>")
#set ($mylist = $mylist + "the unix time for last modification is: " + $modDI + "<br>")

${mylist}

And this is the output of the macro on the page that I inserted it into:

$createOb.setTime($content.currentDate.parse($createD)) $modOb.setTime($content.currentDate.parse($modD)) 
file creation date (before parse): 2018-05-29 14:08:51.904
file modification date (before parse): 2018-06-05 08:51:58.687
creation object time was neither before or after the modify object time
the unix time for creation is: 1528179636921
the unix time for last modification is: 1528179636921

A few questions -

How do I not display the first line of output?

Why are the parsed dates the same (current time)?

Thanx

1 answer

1 accepted

0 votes
Answer accepted
Yaacov Weingarten
Contributor
June 5, 2018

Found the solution to be easier than I thought - since the ContentEntity dates (CreationDate and ModificationDate) actually return a Date object. Used their after() method to perform the comparison

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events