- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted
##

Anonymous

Not applicable

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-22-2018
03:33 PM

05-22-2018
03:33 PM

Velocity Scripting an equation with dates

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:

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

2 REPLIES 2

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-22-2018
06:46 PM

05-22-2018
06:46 PM

Re: Velocity Scripting an equation with dates

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(

$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( $calLeaseCreateDate = $convert.toCalendar(

$convert.parseDate(

$lead.prog_leasecreatedate,

$ISO8601DateOnly,

$defaultLocale,

$defaultTimeZone

)

) )

#set( $daysUntilLeaseCreate = $date.difference($calNow,$calLeaseCreateDate).getDays() )

#if( $daysUntilLeaseCreate > 0 )

#set( $daysOffsetFromBase = $math.sub($DAYS_OFFSET_BASE, $daysUntilLeaseCreate) )

prog_leasecreatedate offset is $daysOffsetFromBase.

#else

prog_leasecreatedate is not in the future.

#end

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

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-27-2018
09:18 AM

07-27-2018
09:18 AM

Re: Velocity Scripting an equation with dates

Thanks again Sanford, that blog post of yours is extremely useful.