SOLVED

Re: Velocity - If/Than Statement for Previous Date

Go to solution
nhabischWings
Level 5

Velocity - If/Then Statement for Previous Date

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.

Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - If/Than Statement for Previous Date

##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.

View solution in original post

10 REPLIES 10
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - If/Than Statement for Previous Date

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

nhabischWings
Level 5

Re: Velocity - If/Than Statement for Previous Date

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
Level 10 - Community Moderator

Re: Velocity - If/Than Statement for Previous Date

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).

nhabischWings
Level 5

Re: Velocity - If/Than Statement for Previous Date

I'm probably overcomplicating this (or totally over my head, but I started by parsing the string into a Date field:

 

#set( $membershipOpeningDate = $convert.parseDate(
    $item.membershipDate,
    $ISO8601DateOnly, 
    $defaultLocale, 
    $defaultTimeZone 
) )

 

 Although when printing the output it prints like "Thu Mar 23 00:00:00 CDT 2023", which seems off from the ISO8601DateOnly I would think it would output. If I print the default $item.membershipDate, it prints out like "2023-03-24".

 

My current attempt is using date.difference like:

##Loops through each field in Customer Profile object
#foreach ( $item in $customerProfilesList )

##Checks to see if Membership Opening Date is yesterday (Due to overnight data load)
#if ( $date.difference($calNow,$membershipOpeningDate).equals(-1) )
$item.fullName##
#else
#end
#end

It's still not outputting properly (giving me the "Else" result every time. I think I am close, but I think it has to maybe do with comparing the format of $calNow and my field? Or else it's probably how I am implementing the equals option.

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - If/Than Statement for Previous Date


@nhabischWings wrote:

I'm probably overcomplicating this (or totally over my head, but I started by parsing the string into a Date field:

#set( $membershipOpeningDate = $convert.parseDate(
    $item.membershipDate,
    $ISO8601DateOnly, 
    $defaultLocale, 
    $defaultTimeZone 
) )

Although when printing the output it prints like "Thu Mar 23 00:00:00 CDT 2023", which seems off from the ISO8601DateOnly I would think it would output. If I print the default $item.membershipDate, it prints out like "2023-03-24".

 $ISO8601DateOnly is the source String format. It's how the Date parser knows which parts of the string are meant to represent years, months, days. It has no effect on the output format when a Date is stringified, which has a default format and also can be customized with $date.format.

 

 

 

 

nhabischWings
Level 5

Re: Velocity - If/Than Statement for Previous Date

Oh okay, so if I'm trying to do a date.difference comparison between the calNow and the parsed date field, do I need to do any more massaging of the field date in order for the difference comparison to work? Or am I formatting it incorrectly?

##Loops through each field in Customer Profile object
#foreach ( $item in $customerProfilesList )

##Checks to see if Membership Opening Date is yesterday (Due to overnight data load)
#if ( $date.difference($calNow,$membershipOpeningDate).equals(-1) )
$item.fullName##
#else
#end
#end

 At least from what I know it should be reading as comparing (2023-03-24, *Membership Opening Date in format YYYY, MM, DD) and if it is 2023-03-24 minus 1, it should work? 

Or does the minus 1 part need more finesse to designate removing one day? 

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - If/Than Statement for Previous Date

$date.difference() (it's not literally $date.diff(), sorry for the confusion as I've been working from my phone the past few days and shortening things) returns a Comparison object.

 

You run the Comparison's .getDays() method to get the difference in days. That's what you compare to -1.

nhabischWings
Level 5

Re: Velocity - If/Than Statement for Previous Date

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.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - If/Than Statement for Previous Date

##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.