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

I would like to compare dates

Kyle Smith March 30, 2015

I want to make a macro that looks at an input date and if that date is in the past, I want to replace it with the current date. Here is what I'm looking at:

## @param inputDate:title=From Date|type=date|desc=Date to start from. Format: MM/dd/yy
#set ($currentDate=$action.dateFormatter.formatGivenString("MM/dd/yy", $content.currentDate))
 
#if ($paraminputDate.before($current_time))
 $currentDate
 #else 
 $paraminputDate
#end

Currently I'm only getting the else. So I'm assuming that I'm using the .before incorrectly. Any help would be appreciated. Thanks.

Edit: I got it working. Here is the code. I am using it for dynamic charts that display schedules.

## @param inputDate:title=From Date|type=date|desc=Date to start from. Format: MM/dd/yy 
#set($inputDate=$content.currentDate) 
$inputDate.setTime($content.currentDate.parse($paraminputDate))
#if ($inputDate.before($content.currentDate))  
$action.dateFormatter.formatGivenString("MM/dd/yy", $content.currentDate)  
#else  
$paraminputDate 
#end

1 answer

1 accepted

1 vote
Answer accepted
Jonathan Simonoff
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.
March 30, 2015

Although the parameter declares its type as "date", I don't think that really means anything – it ends up as a string.  So, you need to get that into a date object to call the .before method.  You can do that with something like the following.  (AFAIK you can't directly declare Java types in Velocity code, so what I do here is do something that makes a variable of the right type, and then change its value.)

#set($inputDate=$content.currentDate)
$inputDate.setTime($content.currentDate.parse($paraminputDate))
#if ($inputDate.before($current_time))
 $currentDate
 #else
 $paraminputDate
#end
Kyle Smith March 30, 2015

That doesn't seem to work. The line "#set ($inputDate=$content.currentDate)" seems to break the macro. I get "Error occurred rendering template content" when I use the macro. I added a space after #set, but I don't really know a lot about the syntax. I removed that line and it no longer errors (obviously not giving me what I'm looking for, but it doesn't error with that line removed).

Jonathan Simonoff
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.
March 30, 2015

I copied that directly from a macro of mine that works. Sorry -- I don't know why it would be erroring for you. The only thing I can think of is that it is a version problem. FWIW, I'm using 5.5. Maybe the .currentDate method of $content isn't there in your version? Hmmm.. That shouldn't actually cause a rendering error, anyway -- when a method isn't recoginized, it is just a string, so that set should work.

Kyle Smith March 30, 2015

Strange. I am on 5.5.2. I'm assuming that you are using your code plus my first 2 lines, is that correct?

Jonathan Simonoff
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.
March 30, 2015

No, my macro does something completely different. I just pulled out the part that set up the comparison and changed it to fit your circumstances. Are you absolutely sure the problem in the line "#set ($inputDate=$content.currentDate)"?

Kyle Smith March 31, 2015

I guess I don't really understand. When I only use your code, I get this error: There are user macros without any metadata configured in their template. They have been highlighted below. For these macros to be available for use in content they require parameter information. See the Guide to User Macro Templates. If I put in my param or no param lines, it doesn't error when I save, but it errors when I use it as a macro. If I remove your line 1, it just displays the text of $inputDate.setTime($content.currentDate.parse($paraminputDate)) but it at least doesn't error out.

Kyle Smith March 31, 2015

After messing with it some more, the .before might be the issue. If I change that line, it doesn't error. I made it .greater and it only gave me the else (for dates before and after), but it didn't error. It also errors with .after.

Kyle Smith March 31, 2015

Awesome! I got it working. I don't know what the issue was, but I'll update my post to include my current code. Thanks for working with me. Your input got me to where I needed. Thanks!

Jonathan Simonoff
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.
March 31, 2015

Ahh, that makes more sense -- I wasn't paying any attention to the comparison part. I wrote my code a while ago and I don't remember why I made these decisions, but I see that at least in places I used .getTime(), which produces an integer value, and then use math to calculate and compare. The .after and .before methods do seem to work for me, though -- I wonder if the problem is the data type of $current_time. Anyway, here's a bigger code snippet, showing comparisons done using getTime and also using .before and .after. #if (!$!paramExpiration) #set($paramExpiration="12/1/2014") #end #set($helper=$action.getHelper()) #set($Integer = 0) #set($today=$content.currentDate) #set($review=$content.currentDate) $review.setTime($content.currentDate.parse($paramExpiration)) #set($valid=$content.currentDate) $valid.setTime($content.currentDate.parse($paramValid)) #set($diff=$review.getTime()-$valid.getTime()) #set($newReview=$content.currentDate) #if($diff>15724800000) ##six months #set($newTime=$valid.getTime()+15724800000) $newReview.setTime($newTime) #set($monthI=$newReview.getMonth()) #set($month=$Integer.parseInt($monthI.toString())+$Integer.parseInt("1")) #set($yearI=$newReview.getYear()) #set($year=$Integer.parseInt($yearI.toString())+$Integer.parseInt("1900")) #set($reviewDate= "${month}/$newReview.getDate()/$year") #set($review=$newReview) #else #set($reviewDate=$paramExpiration) #end <style> #if($today.after($review)) #main{background-color:mistyrose} #elseif ($today.before($valid)) #main{background-color:#FFC966} #end

Jonathan Simonoff
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.
March 31, 2015

Crossed posts -- you don't need that last one :-). Glad you got it working!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events