SOLVED

Velocity - comparing to today's date

Go to solution
Grant_Booth
Level 10

Re: Velocity - comparing to today's date

I think I found a simpler way of doing this. Excuse me if I leave out something obvious because I'm new to velocity, but it's working in my tests and seemed conceptually simpler to me. The idea is that you convert each date to integers, and then simply subtract the two.
I'm trying to determine that a voucher is expired by comparing a custom date field from SFDC with today's date.


##get today's date as yyyyMMdd

#set($todayString = $date.get('yyyyMMdd'))

##make an integer variable
#set($todayInt = 0)

##convert the string to an integer
#set($today = $todayInt.parseInt($todayString))

##remove the dashes from the custom date field to make it yyyyMMdd
#set( $ExpDateString = $Voucher.Expiration_Date__c.replace("-","") )

##make an integer variable
#set( $ExpDateInt = 0)

##convert the expiration date to an integer
#set( $ExpDate = $ExpDateInt.parseInt($ExpDateString) )

##subtract the $today integer from the $ExpDate integer
#if( $ExpDate - $today >= 0 )
This voucher has NOT expired yet
#else
This voucher is EXPIRED
#end

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - comparing to today's date

The difference between 2 unpunctuated ISO 8601 dates *from the same timezone* converted to Integers is indeed a valid way to determine the simple answer to "which one is later."

But date math should be done with proper libraries.

Grant_Booth
Level 10

Re: Velocity - comparing to today's date

And I agree with that sentiment, it's always preferable, but in our use case a margin of error of +/- 1 day is acceptable, so I'm going to save the headache of troubleshooting date objects and using the proper libraries for another day