Hi Everyone,
I’m exploring the best ways to handle dynamic date ranges in Marketo’s Velocity scripting for a use case where opportunities need to be filtered based on whether their close date falls within a specific date range (e.g., today and 1 week prior). I’ve been testing some approaches and would love your insights on the best practices for this.
I opted for text tokens over date tokens because they are easier to compare using .compareTo() in Velocity, avoid complications with time components and formatting, and provide flexibility for preformatted external values. However, I’m open to switching if date tokens can dynamically resolve and better meet the use case.
The use case involves filtering opportunities to show only those with a close date between the current date and a specific range (e.g., 1 week prior). Current tokens are preformatted as yyyy-MM-dd.
I’d love to hear your approaches and recommendations! Thanks in advance for your insights.
Solved! Go to Solution.
I assume you mean Date fields, not Date tokens (as in {{my.tokens}}).
There's no reason to use anything other than Date fields and DateTool ($date
). Formatting of Date fields in Velocity is 100% predictable, and they will be validated on the front end, unlike a String field which can - and inevitably will - contain invalid ISO dates.
If you search my past blog posts I've written extensively about date math and comparison in Velocity.
Hi Sanford,
Thank you for your response and for the clarification on the use of Date fields and DateTool.
So just to be clear I’m using Marketo Date tokens ({{my.currentDate}} and {{my.dateRangeStart}}), formatted as yyyy-MM-dd. While the tokens themselves are valid, $date.parse() in Velocity consistently fails to parse them as proper Date objects, which prevents the script from executing the filtering logic correctly.
I’ve reviewed your blogs and posts about date math and comparison, but I suspect there may be a limitation or caveat when working with Marketo Date tokens in Velocity.
so my questions are
Your insights are greatly appreciated, and thank you for all the guidance you’ve shared with the community!
Hi Sanford,
Thank you for the clarification and the link to your post - it was exactly what I needed.
Your explanation and article have cleared up the issue, and I’ll adjust the script to properly preprocess the tokens before use. I really appreciate your time and expertise—thanks again for pointing me in the right direction!