calendarvelocitymarketo

Find the end of current month with Velocity


I've just started to use Marketo's mail scripting, and I need to find out the last of the current month.

I could find the current date as below.How can I do further?

#set($date = $date.calendar)
#set($current_date = $date.format('yyyy-mm-dd', $date.getTime()))
$current_date

Refer to here, Subtract months from date in velocity I tried to subtract 1 day from the beginning of next month, but it doesn't work.

Knowing the number of days in this month is also meets the requirement.


Solution

  • The method you tried should work. I think that your problem is that you are overwriting $date - which initially contains the DateTool - with your working variable. When Velocity Tools org.apache.velocity.tools.userCanOverwriteTools configuration value is true (which is the default) Velocity will let you overwrite $date but the DateTool will be unavailable thereafter.

    So try changing your working variable to $cal for instance. Then, you have several methods:

    #set($cal = $date.calendar)
    $cal.add(2, 1)
    $cal.set(5, 1)
    $cal.add(5, -1)
    $date.format('yyyy-MM-dd', $cal)
    

    or

    #set($cal = $date.calendar)
    $cal.set(5, $cal.getActualMaximum(5))
    $date.format('yyyy-MM-dd', $cal)
    

    In all cases, you resort to using the Calendar.MONTH and Calendar.DATE constants (respectively 2 and 5). You may want to put such utility operations in a Java tool of your own to have more readable templates.