Date Math in V1 Email Templates

Is it possible to do date math in the V1 Email Templates?
Looking to get seven (7) days prior to the current date…

#set($dateObject=$spTools.getClass().forName("java.util.Date").newInstance()) 
#set($formattedDate=$spTools.formatDate($dateObject,"MM/dd/yyyy HH:mm"))
Current Date:$dateObject
Formatted Date:$formattedDate

Using the above from @officialamitguptaa , how can i get the Current Date to be seven days prior?

everything i have seen and read, has noty worked, and the Velocity site is limited in Date Math (maybe for a reason???)

Could you try this ?

#set($spToolsClass = $spTools.getClass())
#set($dateClass = $spToolsClass.forName("java.util.Date"))
#set($calendarClass = $spToolsClass.forName("java.util.Calendar"))
#set($dateObject = $dateClass.newInstance())

#set($calendarInstance = $calendarClass.getInstance())
$calendarInstance.setTime($dateObject)
$calendarInstance.add($calendarClass.DAY_OF_MONTH, 7)

#set($formattedDate = $spTools.formatDate($calendarInstance.getTime(), "MM/dd/yyyy HH:mm"))

Current Date: $dateObject
Formatted Date: $formattedDate

Hi @masd1 -

Use the below snippet, Its working -

#set($spToolsClass = $spTools.getClass())
#set($dateClass = $spToolsClass.forName("java.util.Date"))
#set($calendarClass = $spToolsClass.forName("java.util.Calendar"))
#set($dateObject = $dateClass.newInstance())

#set($calendarInstance = $calendarClass.getInstance())
$calendarInstance.setTime($dateObject)
$calendarInstance.add(5,-7) 

#set($formattedDate = $spTools.formatDate($calendarInstance.getTime(), "MM/dd/yyyy HH:mm"))

Current Date: $dateObject
Formatted Date: $formattedDate

@baoussounda - You were close.

Please note - Here 5 is constant static value representing DATE. For more information refer the below Java doc from Oracle Java. You can use -7 to subtract 7 days from the current Date.

$calendarInstance.add(5,-7) 

Mark it as solved, If it helps.

1 Like

Perfect and am seeing it working as expected!!!
thanks again @officialamitguptaa

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.