Velocity - If/Then Statement for Previous Date | Community
Skip to main content
New Participant
March 22, 2023
Solved

Velocity - If/Then Statement for Previous Date

  • March 22, 2023
  • 1 reply
  • 3658 views

Hello!
Back with another (likely) basic question I can't seem to parse:

 

##Standard Velocity Date/Time Fields #set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Chicago") ) #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" ) ##Looks for membership date of today and prints Member full name #foreach ( $item in $customerProfilesList ) #if ( $item.membershipDate.equals($calNow.add($calConst.DATE,-1)) ) $item.fullName## #else #end #end

 

Trying to write a script that takes a DATE field (formatted as YYYY-MM-DD) and if it matches yesterday's date - prints the Full Name from that particular record.
It doesn't throw an error when I preview or test, but it doesn't work. I had got it to work using the current date, but our data source currently updates overnight so there's a 24-hour lag time unfortunately.

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

Sorry for all the back and forth, this is my latest full script:

##Loops through each field in Customer Profile object #foreach ( $item in $customerProfilesList ) ##Standard Velocity Date/Time Fields #set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Chicago") ) #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( $membershipOpeningDate = $convert.parseDate( $item.membershipDate, $ISO8601DateOnly, $defaultLocale, $defaultTimeZone ) ) ##Looks to see if Membership date is yesterday, due to data overnight upload #if ( $date.difference($calNow,$membershipOpeningDate).getDays().equals(-1) ) $item.fullName #else #end #end

 

I think this should be correct, but it's still resulting in my else result - even when testing against a Membership Date of 2023-03-23.


##Standard Velocity Date/Time Fields #set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Chicago") ) #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" ) ## iterate Customer Profiles, looking for those with Membership Opening yesterday #foreach ( $item in $customerProfilesList ) #set( $membershipOpeningDate = $convert.parseDate( $item.membershipDate, $ISO8601DateOnly, $defaultLocale, $defaultTimeZone ) ) ##Looks to see if Membership date is yesterday, due to data overnight upload #if ( $date.difference($calNow,$membershipOpeningDate).getDays().intValue().equals(-1) ) $item.fullName #else #end #end

getDays() returns a Long.

 

You also shouldn't repeatedly declare the Date constants. That's a one-time thing.

1 reply

SanfordWhiteman
New Participant
March 22, 2023

You want $date.difference. Search my past posts for examples.

New Participant
March 23, 2023

Ah makes sense, though is this able to go "if current date is 2023-03-23, look for "2023-03-22" or is it more giving "difference in date is -1"? I tried the below and it's not broken but not populating correctly:

##Looks for membership date of today and prints Member full name #foreach ( $item in $customerProfilesList ) #if ( $date.difference($calNow,$item.membershipDate).getDays().equals(-1) ) $item.fullName #else #end #end

Probably poor syntax, but all I can find with date.difference documentation is taking a set date differencing a set date (like promo start/end date) and not necessarily going "take date from field record and add/subtract".

Sorry, I know this should be super basic, but the date stuff is tricky.

SanfordWhiteman
New Participant
March 23, 2023

The $date.difference() between two Dates tells you whether one is the day before the other. There's no need to precompute a "yesterday" Date object and compare equality, since that's the same as "diff between this Date and then is -1 days".

 

$date.whenIs() is also a shortcut for "diff between today and then" although I usually avoid it to ensure time zone alignment.

However, you have to compare Date objects. You're trying to compare a Date and a String (no matter how Date-like a String is, it's not a Date yet).