AC Personalization blocks date format issue in AEM Html template | Community
Skip to main content
Level 2
April 30, 2023
Solved

AC Personalization blocks date format issue in AEM Html template

  • April 30, 2023
  • 3 replies
  • 1372 views

Hi Team,

does anyone know how to convert date format (from YYYY-DD-MM to MM/DD/YYYY) in AC custom Personalization blocks in AEM Template?   

We pull the AC email delivery content from AEM and use it in campaign. Issue is when i include the AC Personalization block in AEM Template. I want that PB dynamically populate the value in this format MM/DD/YYYY to recipient email. AC side it stores in YYYY-DD-MM postgres default date format.

I don't want to convert the date in AC using date format method. I want to convert it from AEM template. 

Reason is if i make the conversation at PB level at AC side, it will fix the date format issue. but i can't use that PB for different countries date format.

is there any way it handles it AEM side through some method which can force it to delivered in MM/DD/YYYY ?

PB : <%= targetData.purchase_date %> 

 

any idea community expert: @akshaaga @marcel_szimonisz @parthasarathy @david--garcia @_manoj_kumar_ 

Thanks!!!

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 ShashankNigam02

@pradyumn, You can define a custom method in PB and invoke it to get the correct format data:

 

<% function getDateInLocalFormat(dt){ var cc = recipient.countryCode; if(cc == 'IN'){ // return IN format date }else if(cc == 'US'){ // return US format date }else{ // return generic format date } %> <%=getDateInLocalFormat(targetData.purchase_date)%>

 

3 replies

ShashankNigam02
Adobe Employee
ShashankNigam02Adobe EmployeeAccepted solution
Adobe Employee
April 30, 2023

@pradyumn, You can define a custom method in PB and invoke it to get the correct format data:

 

<% function getDateInLocalFormat(dt){ var cc = recipient.countryCode; if(cc == 'IN'){ // return IN format date }else if(cc == 'US'){ // return US format date }else{ // return generic format date } %> <%=getDateInLocalFormat(targetData.purchase_date)%>

 

PradyumnAuthor
Level 2
April 30, 2023

Thanks @shashanknigam02  , I will try this approach and let you know results.

akshaaga
Adobe Employee
Adobe Employee
May 2, 2023

@pradyumn,

You can convert the date format from YYYY-DD-MM to MM/DD/YYYY in your AEM Template using the SimpleDateFormat class in Java.

As an example-

<%

  String purchaseDateStr = targetData.purchase_date;

  SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-dd-MM");

  SimpleDateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");

  Date purchaseDate = inputFormat.parse(purchaseDateStr);

  String formattedDate = outputFormat.format(purchaseDate);

%>

<%= formattedDate %>

In this code, we first get the purchase_date value from the targetData object. We then create two SimpleDateFormat objects - inputFormat for parsing the input date string, and outputFormat for formatting the output date string. We then parse the input date string into a Date object using the inputFormat object.

Finally, we format the Date object into a string using the outputFormat object, and store the formatted string in the formattedDate variable.

You can then use the formattedDate variable wherever you need to display the formatted date in your AEM Template.

Note that the code assumes that the purchase_date value is always in the format "YYYY-DD-MM". If there is a possibility that the input format might vary, you may need to add some error handling to ensure that the parsing and formatting is done correctly.

Sukrity_Wadhwa
Community Manager
Community Manager
May 11, 2023

Hi @pradyumn,

Were you able to resolve this query with the help of the given solutions or do you still need more help here? Do let us know. In case the given solutions were helpful, then kindly choose the one that helped you the most as the 'Correct Reply'.
Thanks!

Sukrity Wadhwa
PradyumnAuthor
Level 2
May 15, 2023

Fixed that issue at upstream data provider system. same being used in PB to populate in email. Technically both solutions are correct.

Sukrity_Wadhwa
Community Manager
Community Manager
May 15, 2023

Thanks for sharing with the Community! 🙂

Sukrity Wadhwa