Velocity Script Issue | Community
Skip to main content
New Participant
August 14, 2018
Solved

Velocity Script Issue

  • August 14, 2018
  • 2 replies
  • 6004 views

I am using this script to give an expiration date that is one week after an email is sent. I can't figure out how to add the "th, nd, st etc" to the date. For example, August 20th instead of August 20. I am having a hard time figuring out how to do this, any help would be appreciated.

#set($dateInSevenDays = $date.calendar)

$dateInSevenDays.add(10,168)

$date.format('EEEE, MMMM d',$dateInSevenDays.time)

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

Exactly like so:

#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/New_York") )

#set( $defaultLocale = $date.getLocale() )

#set( $calNow = $date.getCalendar() )

#set( $ret = $calNow.setTimeZone($defaultTimeZone) )

#set( $calConst = $field.in($calNow) )

#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )

#set( $ISO8601DateOnly = "yyyy-MM-dd" )

#define( $enUSDayOrdinalIndicators )

1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th,11th,12th,13th,14th,15th,16th,17th,18th,19th,20th,21st,22nd,23rd,24th,25th,26th,27th,28th,29th,30th,31st

#end

#set( $indicatorList = $enUSDayOrdinalIndicators.toString().trim().split(",?\d+") )

Today with a friendly ordinal indicator is

${date.format(

  "EEEE, MMMM d'${indicatorList[$calNow.get($calConst.DAY_OF_MONTH)]}'",

  $calNow,

  $defaultLocale,

  $defaultTimeZone

)}

Here I'm displaying the current date using the ordinal indicator.  You can see elsewhere in the post how to add a certain number of days, and simply use that approach with the $date.format() shown here.  (Note you always must set a timezone or half the world will have errors.) 

2 replies

Chris_Wilcox
New Participant
August 14, 2018

Reading the documentation that @Sanford Whiteman​ linked, I'm not sure if there's a simple way.

All I can think would be to first calculate what the month would be as 1 value, calculate what the date would be as a second value, then essentially map the values to their "th, nd, st" counterparts with an if statement based on the returned 'date' numeric value, then combine the two in your final

Not super easy, but that looks to me like the only way to do it...

New Participant
August 14, 2018

Thanks Chris, it's a bit over my head so I was trying to find something simple but it does look like that is the only way that I'll be able to do make it work.

Chris_Wilcox
New Participant
August 14, 2018

It's unfortunate because the output of your date format has to fit within the velocity rules, so you can't just modify it to add the "th" yourself. You'd have to split it to first return the month, then return the day. Then basically do:

#if $dayOfMonth = 1

     1st

#elseif $dayOfMonth = 2

     2nd

#elseif $dayOfMonth = 3

     3rd

etc...

Not real fun, but that should (might?) work!

SanfordWhiteman
New Participant
August 14, 2018

Please read the seminal post on days and times in Velocity: https://blog.teknkl.com/velocity-days-and-weeks/