0

I am looking to show the date in two different formats based on locale (the locale variable is set to LocaleCode earlier in the email). For en-US it should be January 1, 2021 and all other locales it should be 1 January, 2021. The actual date is being pulled from a data extension and I have it formatted for en-US currently. What I am trying to do is change the format based on the locale. Here is what I have tried (among other things) but it's still not displaying.

TrialEndDate pulls a date from a data extension: SET @TrialEndDate = [ExpirationDate]

%%[
 
    IF @locale == "en-US" then
    SET @date=TreatAsContent(FormatDate(@TrialEndDate, "MMMM d, yyyy"))
    ELSE
    SET @date=TreatAsContent(FormatDate(@TrialEndDate, "d MMMM, yyyy"))
    ENDIF
 
]%%
%%=v(@date)=%%

I had also tried this, without the TreatAsContent function:

%%[

    IF @locale == "en-US" then
    SET @date="%%=FormatDate(@TrialEndDate, "MMMM d, yyyy")=%%"
    ELSE
    SET @date="%%=FormatDate(@TrialEndDate, "d MMMM, yyyy")=%%"
    ENDIF

]%%
%%=v(@date)=%%
4
  • The treatascontent() function isn't needed. And can you update your question with a sample of the what's in@TrialEndDate? Dec 17, 2020 at 20:28
  • Question updated.
    – highbranch
    Dec 17, 2020 at 20:38
  • Is [ExpirationDate] a Date data type? Dec 17, 2020 at 21:14
  • Yes. It currently is in the DE as 2021-01-01 12:00 PM but I have reformatted it to display as mentioned in my original question.
    – highbranch
    Dec 17, 2020 at 21:41

1 Answer 1

1

You should definitely wrap your send context personalization strings so you can check for empty.

Also, the pattern for the day of the month is dd.

I'd recommend something like this:

%%[

  set @debug = 1
  set @trialEndDate = AttributeValue("ExpirationDate")
  set @locale = AttributeValue("locale")

  if @debug == 1 then
    output(concat("<br>trialEndDate: ", @trialEndDate))
    output(concat("<br>locale: ", @locale))
  endif

  if not empty(@trialEndDate) then

    IF @locale == "en-US" then

       SET @date = FormatDate(@TrialEndDate, "MMMM dd, YYYY")

    ELSE

       SET @date = FormatDate(@TrialEndDate, "dd MMMM, YYYY")

    ENDIF

  else

    set @date = "unknown"

  endif

]%%
%%=v(@date)=%%
6
  • Thanks, I will try this. But won't dd display as 01? The official SF developer documentation has a single 'd' as a token option. developer.salesforce.com/docs/…
    – highbranch
    Dec 17, 2020 at 21:48
  • I'd test it with static dates set @trialEndDate = "2020-10-10" and set @trialEndDate = "2020-10-01" Dec 17, 2020 at 22:04
  • Still not having any luck. Does AttributeValue pull the value from the DE? Also, do all the @trialEndDate references need to match when it comes case sensitivity?
    – highbranch
    Dec 17, 2020 at 23:15
  • AttributeValue reads values from the send context. AMPscript is case-insensitive. What is the output from my code? Dec 18, 2020 at 3:35
  • There is no output. It is blank.
    – highbranch
    Dec 18, 2020 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.