Re: Formatting / Subtracting Date - Velocity stopped working

Anonymous
Not applicable

Formatting / Subtracting Date - Velocity stopped working

I have a Velocity script where I use DateTool and ComparisonDateTool to find the difference between today and the day a lead started a trial.  It worked magically one day.  And the next day it didn't.  The value holding the start day of the trial would no longer convert into the proper Date Object thus the ComparisonDate difference() would not work. 

There were no changes to the field or our Marketo that I was aware of.  Anyone have any thoughts?  Snippet below

#set ($today = $date.getDate())

#set ($start = $date.format(${OpportunityList.get(0).StartDate__c}))

#set ($diff = $date.difference($start, $today).days)

z${today} | ${start} | ${diff}

5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: Formatting / Subtracting Date - Velocity stopped working

You'll need to provide more information about the failure.  Is StartDate__c a string value to begin with, or an actual Date object? If a string, what's the exact format?

Fundamentally $date.format() isn't the way to go about creating date objects for comparison, since it is designed for output (Date-like String → Date → Date-like String).  You want a Date object here, not its default string representation.

Anonymous
Not applicable

Re: Formatting / Subtracting Date - Velocity stopped working

StartDate__c outputs "2017-03-01" and is a Date field from SFDC.  But I don't think its get read as a Velocity Date Object.

I've tried setting $start by using $date.toDate() and $convert.parseDate()to achieve the proper conversion, but it comes back empty.

Anonymous
Not applicable

Re: Formatting / Subtracting Date - Velocity stopped working

I found your article on http://blog.teknkl.com/marketo-vtl-strings-all-the-way-down/ and had an ah-ha moment!  Thanks and tons of props!

For anyone trying to solve a similar issue, $convert.parseDate()worked when I used the proper time zone to convert my StartDate__c field into a Date Object.  I was then able to do a proper date comparison using $date.difference().

SanfordWhiteman
Level 10 - Community Moderator

Re: Formatting / Subtracting Date - Velocity stopped working

Hey, great! I'd been typing up a longer response to you in another tab but if you're all set, even better.

There's still a potential problem in your code if you're not using the same timezones for the system date and for your user-supplied date (when you represent Date differences as full days, that means the difference can be off by 1, depending on the time of a send).

I plan to add some of this stuff officially to my Snippets collection soon. For some reason even though timezones are my obsession I tend to repost the snippets to the Community instead.

Anonymous
Not applicable

Re: Formatting / Subtracting Date - Velocity stopped working

Yup!  Both dates should be Central Time and I've updated both as such.  Thank you for your contributions.  Wouldn't it be easier to link to a Snippet instead of reposting?