Hello,
I'm trying to figure out how to format a date on a custom object. I've seen other posts, but am not finding one that is addressing my specific use case... and if it is, I'm not understanding what I need to change to make it work.
Here is what I started with and it is pulling the date, just not in the format I want/need:
#if( !$customList_cList.isEmpty() )
#foreach( $customList in $customList_cList )
#if( $customList.listID.equals("Returned Item") && !$customList.date1.isEmpty() )
#set( $myDate = $customList.date1 )
#break
#end
#end
#end
${myDate}
Looking at the community and seeing posts from @SanfordWhiteman, I know that I need to incorporate time zones and the like... I found a post that looked similar to what I wanted to do and I ended up with this:
#set( $dateOptions = {
"formats" : {
"userin" : "yyyy-MM-dd",
"userout" : "m d, yyyy"
},
"timezones" : {
"userin" : "America/Los_Angeles",
"userout" : "America/Los_Angeles"
},
"locale" : $date.getLocale()
} )
#set( $transactionDatelike = $customList_cList.get($lastIndex).date1 )
#set( $transactiontDate = $convert.parseDate(
$transactionDatelike,
$dateOptions.formats.userin,
$dateOptions.locale,
$date.getTimeZone().getTimeZone($dateOptions.timezones.userin)
) )
## convert the date to a different format
#set( $transactionDate_formatted = $date.format(
$dateOptions.formats.userout,
$transactionDate,
$dateOptions.locale,
$date.getTimeZone().getTimeZone($dateOptions.timezones.userout)
) )
$transactionDate_formatted
But I know this isn't going to work, because it's not looking for the Custom List with a List ID of "Returned Item", and it's just spitting out the token value at the end when I test it.
Advice on how to fix this so that it displays the date in m d, yyyy format?
Solved! Go to Solution.
That won’t compile as it has broken references and is missing a closing #end. The date output format probably isn’t what you want, either.
Make sure to pay detailed attention to variable names and overall nesting, coding is about attention to detail above all else.
More like:
#set( $dateOptions = {
"formats" : {
"userin" : "yyyy-MM-dd",
"userout" : "MMM d, yyyy"
},
"timezones" : {
"userin" : "America/Los_Angeles",
"userout" : "America/Los_Angeles"
},
"locale" : $date.getLocale()
} )
#foreach( $customList in $customList_cList )
#if( $customList.listID.equals("Returned Item") && !$customList.date1.isEmpty() )
#set( $myDatelike = $customList.date1 )
#set( $myDate = $convert.parseDate(
$myDatelike,
$dateOptions.formats.userin,
$dateOptions.locale,
$date.getTimeZone().getTimeZone($dateOptions.timezones.userin)
) )
#set( $myDate_formatted = $date.format(
$dateOptions.formats.userout,
$myDate,
$dateOptions.locale,
$date.getTimeZone().getTimeZone($dateOptions.timezones.userout)
) )
#break
#end
#end
${myDate_formatted}
But I know this isn't going to work, because it's not looking for the Custom List with a List ID of "Returned Item", and it's just spitting out the token value at the end when I test it.
More to the point, it uses a variable $lastIndex which you never defined.
Here is what I started with and it is pulling the date, just not in the format I want/need:#if( $customList.listID.equals("Returned Item") && !$customList.date1.isEmpty() )
Not sure you really want isEmpty() here, you may mean to check for null or empty (custom object fields are nullable).
In any case, when the first loop exits you have a variable $myDate. So why are you referring to another variable here? This is where you should be using $myDate — input/parse it, then output it in the desired format.
#set( $transactionDatelike = $customList_cList.get($lastIndex).date1 )
I had abandoned the first script because it wasn't accounting for the timezone, which I want to do.
What do I need to include to include checking for null values?
Are you saying these should be combined? something like (I know this is not correct because it's not rendering the email):
#foreach( $customList in $customList_cList )
#if( $customList.listID.equals("Returned Item") && !$customList.date1.isEmpty() )
#set( $myDate = $customList.date1 )
#set( $dateOptions = {
"formats" : {
"userin" : "yyyy-MM-dd",
"userout" : "m d, yyyy"
},
"timezones" : {
"userin" : "America/Los_Angeles",
"userout" : "America/Los_Angeles"
},
"locale" : $date.getLocale()
} )
#set( $myDatelike = $customList.date1)
#set( $myDate = $convert.parseDate(
$myDatelike,
$dateOptions.formats.userin,
$dateOptions.locale,
$date.getTimeZone().getTimeZone($dateOptions.timezones.userin)
) )
#set( $myDate_formatted = $date.format(
$dateOptions.formats.userout,
$myDate,
$dateOptions.locale,
$date.getTimeZone().getTimeZone($dateOptions.timezones.userout)
) )
$transactionDate_formatted
That won’t compile as it has broken references and is missing a closing #end. The date output format probably isn’t what you want, either.
Make sure to pay detailed attention to variable names and overall nesting, coding is about attention to detail above all else.
More like:
#set( $dateOptions = {
"formats" : {
"userin" : "yyyy-MM-dd",
"userout" : "MMM d, yyyy"
},
"timezones" : {
"userin" : "America/Los_Angeles",
"userout" : "America/Los_Angeles"
},
"locale" : $date.getLocale()
} )
#foreach( $customList in $customList_cList )
#if( $customList.listID.equals("Returned Item") && !$customList.date1.isEmpty() )
#set( $myDatelike = $customList.date1 )
#set( $myDate = $convert.parseDate(
$myDatelike,
$dateOptions.formats.userin,
$dateOptions.locale,
$date.getTimeZone().getTimeZone($dateOptions.timezones.userin)
) )
#set( $myDate_formatted = $date.format(
$dateOptions.formats.userout,
$myDate,
$dateOptions.locale,
$date.getTimeZone().getTimeZone($dateOptions.timezones.userout)
) )
#break
#end
#end
${myDate_formatted}
Works beautifully. Thank you so much.
I see where I went wrong now.