Velocity Code Date Subtract with Math | Community
Skip to main content
New Participant
July 1, 2021
Solved

Velocity Code Date Subtract with Math

  • July 1, 2021
  • 1 reply
  • 4070 views

Hi. We have a membership-based organization.

 

We have field data for $lead.aornPaidThru date. This is the member's renewal date.

 

I am trying to subtract 89 days from the $lead.aornPaidThru date to display a date that's 90 days in advance of the member's renewal date.

 

I'm not that familiar with Velocity (probably obvious).

 

Here's what I've put together, but it is not working. I do not have a developer available to assist with this, so I'm hoping for a little help here from the community, please.

 

When I run this in a Marketo email with a member, all that displays for the token result is: 

aornPaidThru is not in the future

 

 

 

#set( $DAYS_OFFSET_BASE = 89 ) #set( $ret = $calNow.set( $calNow.get($calConst.YEAR), $calNow.get($calConst.MONTH), $calNow.get($calConst.DAY_OF_MONTH), 0, 0, 0 ) ) #set( $ret = $calNow.set($calConst.MILLISECOND,0) ) #set( $calRenewDate = $convert.toCalendar( $convert.parseDate( $lead.aornPaidThru, $ISO8601DateOnly, $defaultLocale, $defaultTimeZone ) ) ) #set( $daysUntilRenewDate = $date.difference($calNow,$calRenewDate).getDays() ) #if( $daysUntilRenewDate > 0 ) #set( $daysOffsetFromBase = $math.sub($DAYS_OFFSET_BASE, $daysUntilRenewDate) ) aornPaidThru offset is $daysOffsetFromBase. #else aornPaidThru is not in the future. #end

 

 

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

I appreciate your help and patience with me as I learn this.

 

My intended logic is if a member's PaidThruDate is, for example 2022-04-30, then the calculation would substract 90 days and display that date, 2022-01-30.



My intended logic is if a member's PaidThruDate is, for example 2022-04-30, then the calculation would substract 90 days and display that date, 2022-01-30.

Then you want

## standard date math includes #set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/New_York") ) #set( $defaultLocale = $date.getLocale() ) #set( $calNow = $date.getCalendar() ) #set( $ret = $calNow.setTimeZone($defaultTimeZone) ) #set( $calConst = $field.in($calNow) ) #set( $ISO8601DateOnly = "yyyy-MM-dd" ) #set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" ) #set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" ) #set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ) #set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) ## #set( $DAYS_OFFSET_BASE = 90 ) #set( $ret = $calNow.set( $calNow.get($calConst.YEAR), $calNow.get($calConst.MONTH), $calNow.get($calConst.DAY_OF_MONTH), 0, 0, 0 ) ) #set( $ret = $calNow.set($calConst.MILLISECOND,0) ) #set( $calRenewDate = $convert.toCalendar( $convert.parseDate( $lead.aornPaidThru, $ISO8601DateOnly, $defaultLocale, $defaultTimeZone ) ) ) #if( $date.difference($calNow,$calRenewDate).getDays() > 0 ) #set( $isRenewDateInFuture = true ) #else #set( $isRenewDateInFuture = false ) #end #if( $isRenewDateInFuture ) #set( $calOffset = $calRenewDate.clone() ) #set( $void = $calOffset.add($calConst.DATE,-90) ) Paid through ${date.format($ISO8601DateOnly,$calOffset,$defaultLocale,$defaultTimeZone)}. #else Renewal date is not in the future. #end

1 reply

SanfordWhiteman
New Participant
July 1, 2021
I assume you have my standard date math includes above that? There are already broken references if not.
Please show the full code from all relevant tokens, then I can show you how it's done.
gabbottAuthor
New Participant
July 1, 2021

This is as far as I got.

George Abbott
SanfordWhiteman
New Participant
July 1, 2021

Well, you need to include the includes from Always include this bit, or references like $calNow don’t have any meaning!

## standard date math includes #set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/New_York") ) #set( $defaultLocale = $date.getLocale() ) #set( $calNow = $date.getCalendar() ) #set( $ret = $calNow.setTimeZone($defaultTimeZone) ) #set( $calConst = $field.in($calNow) ) #set( $ISO8601DateOnly = "yyyy-MM-dd" ) #set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" ) #set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" ) #set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ) #set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) ## #set( $DAYS_OFFSET_BASE = 89 ) #set( $ret = $calNow.set( $calNow.get($calConst.YEAR), $calNow.get($calConst.MONTH), $calNow.get($calConst.DAY_OF_MONTH), 0, 0, 0 ) ) #set( $ret = $calNow.set($calConst.MILLISECOND,0) ) #set( $calRenewDate = $convert.toCalendar( $convert.parseDate( $lead.aornPaidThru, $ISO8601DateOnly, $defaultLocale, $defaultTimeZone ) ) ) #set( $daysUntilRenewDate = $date.difference($calNow,$calRenewDate).getDays() ) #if( $daysUntilRenewDate > 0 ) #set( $daysOffsetFromBase = $math.sub($DAYS_OFFSET_BASE, $daysUntilRenewDate) ) aornPaidThru offset is $daysOffsetFromBase. #else aornPaidThru is not in the future. #end

 

I’m not actually sure what your intended logic is, but if the $lead.aornPaidThru is “2021-07-08” (a week from now) then $daysOffsetFromBase is 82.