2 Replies Latest reply on Jul 27, 2018 9:18 AM by Phillip Wild

    Velocity Scripting an equation with dates

    Katey Wang

      Here is what I'm trying to do:


      Find the difference between todays date, and the lease creation date.

      Subtract the difference from 89 days.

      Display that number.


      Here is what I have so far:

      Screen Shot 2018-05-22 at 4.31.08 PM.png


      I just can't get the "minus 89 days" to work.

        • Re: Velocity Scripting an equation with dates
          Sanford Whiteman

          You should read this seminal post on days and times in Velocity, where I explain that you must always include a timezone when parsing dates.


          In particular, this section explains how to use $date.difference without surprising shifts of one day in either direction.


          Once you set the tz (by including the boilerplate code at the top of the post), you simply call $math.sub:


          #set( $DAYS_OFFSET_BASE = 89 )
          #set( $ret = $calNow.set(
          ) )
          #set( $ret = $calNow.set($calConst.MILLISECOND,0) )
          #set( $calLeaseCreateDate = $convert.toCalendar(
          ) )
          #set( $daysUntilLeaseCreate = $date.difference($calNow,$calLeaseCreateDate).getDays() )
          #if(  $daysUntilLeaseCreate > 0 )
          #set( $daysOffsetFromBase = $math.sub($DAYS_OFFSET_BASE, $daysUntilLeaseCreate) )
          prog_leasecreatedate offset is $daysOffsetFromBase.
          prog_leasecreatedate is not in the future.


          Finally, when posting code, please use the syntax highlighter in Advanced Editor, not a screenshot:



          2 of 2 people found this helpful