Expand my Community achievements bar.

SOLVED

Dynamic Date in Subject and email body

Avatar

Level 2

Hi everyone,

I'm new to Adobe Campaign Manager and I'm trying to use dynamic date in the email subject and email body. By dynamic date I meant is - days in month followed by month name - i.e. 30 September, 31 October and so on. Also, needs to consider February as a special case. Can anyone please tell me how I can do this in the email template? I can create HTML file and use javascript within the HTML but I'm not sure whether it is permissible to use such HTML as email delivery HTML in ACM. Also, what can be done for subject line with such dynamic date ? 

Your help and guidance will really help me.

1 Accepted Solution

Avatar

Correct answer by
Level 6

Another implementation way

<%
function daysInMonth(today) {
     return new Date(today.getFullYear(), today.getMonth(), 0).getDate();
}
var today = new Date();
%>

<%= daysInMonth(today) %> <%= formatDate (today, "%Bl") %>

 

View solution in original post

36 Replies

Avatar

Level 2

Thank you for your reply. How to use format date for dynamic month in subject? Also, how to fetch number of days in a particular month and current month as dynamic one in the email body?

Avatar

Community Advisor

Hi @vinay049 

You can try the following.

It works for me.

 

Subject:  welcome email - <%= formatDate(new Date(), "%Bl") %>

Body: <%= formatDate(new Date(), "%2D %Bl %4Y") %> 

 

Cheers,

Deb

Avatar

Level 2

Thank you for your reply, Deb.

 

For subject line - this will work.

 

For body - let me clarify what I'm trying to do. I want to display max number of days in the current month, current month name and current year. For example, 30 September, 31 October, 30 November and so on. February - it will be trickier month. I'm trying to figure out how to do this.

Avatar

Level 2

Hi Anthony,

 

Tank you so much for your reply and suggestion.

 

I think I need to clarify the question. I want to do 2 things:

 

1. I want to display current month name in the email subject For example, welcome email - September

 

2. I want to display number of days in the current month, month name and year in the email body. For example, 30 September 2021.

 

This should be dynamic. I'm trying to figure out how to do this in ACM - Email Delivery - HTML file for email body and email subject within the Email Delivery option.

 

Can you able to help me out please ?

Avatar

Level 2

In your subject line, use the code provided.  Example:

Subject Line: Daily Newsletter - <%=formatDate(getCurrentDate(),"%2D %Bl %4Y")%>

When you test, you'll see the subject line will say:

 

"Daily Newsletter - 23 September 2021"

Avatar

Level 2

Thank you for your reply, Anthony.

 

For subject line - this will work.

 

For body - let me clarify what I'm trying to do. I want to display max number of days in the current month, current month name and current year. For example, 30 September, 31 October, 30 November and so on. February - it will be trickier month. I'm trying to figure out how to do this. 

 

Do you have any idea for this?

Avatar

Level 2

Try this.  It will get the "0th" day of the month, which is really the last day of the prior month.

<%
var d = new Date()
var m = d.getMonth()
var y = d.getFullYear()
var lastDate = new Date(y, m + 1, 0);
var dateString = formatDate(lastDate,"%2D %Bl %4Y")
document.write(dateString)
%>

Avatar

Level 2

Hi Anthony,

 

I have tried to use the code but I don't receive test emails at all after using it. I have used the code in the email body HTML as suggested above. Do I need to put this somewhere else? also, do I need to add any syntax?

 

For example,

I have used this snippet like - <div> This is test snippet and you need to complete this test by <% var d = new Date() var m = d.getMonth() var y = d.getFullYear() var lastDate = new Date(y, m + 1, 0); var dateString = formatDate(lastDate,"%2D %Bl %4Y") document.write(dateString) %> </div>

 

But I'm not getting any test emails and there isn't any error when I click on Analyze delivery in ACM.

 

Can you please help ?

Avatar

Level 2

I tested the code and got "30 September 2021"

 

Can you remove the code and confirm you receive the email?  Then add the code back and confirm the code is the cause of the issue.

 

Also, make sure the code is not on a single line.  Each line of code must be on a separate line.  Otherwise, use this: 

 

<% var d = new Date(); var m = d.getMonth(); var y = d.getFullYear(); var lastDate = new Date(y, m + 1, 0); var dateString = formatDate(lastDate,"%2D %Bl %4Y"); document.write(dateString) %>

 

The semi-colons will allow it to run as a single line.

Avatar

Level 2

Yes, I have followed the steps mentioned by you.

 

Used ";" in between, but it didn't work.

 

Removed that snippet and sent test email, Got email.

 

But when I put that snippet in, not able to receive the email.

 

I have used something like this 

 

<div>This is test email and needs to be done by <% var d = new Date(); var m = d.getMonth(); var y = d.getFullYear(); var lastDate = new Date(y, m + 1, 0); var dateString = formatDate(lastDate,"%2D %Bl %4Y"); document.write(dateString) %></div>

 

 

Avatar

Level 2

I just tried this and it previews correctly:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
</HEAD>
<BODY>
<DIV>This is test email and needs to be done by <%
 var d = new Date(); var m = d.getMonth(); var y = d.getFullYear(); var lastDate = new Date(y, m + 1, 0); var dateString = formatDate(lastDate,"%2D %Bl %4Y"); %>:
<%=dateString%>  Please see to it!</DIV></BODY></HTML>

2021-09-24_17-04-20.png

Avatar

Level 2

I'm not sure what's the issue.

 

I have removed the entire email.

 

And created simple HTML using the code given above. I didn't receive the test email. Is there any configuration related issue in MS Outlook for receiving email written with JS or configuration issue in ACM or anyother issue?

 

Because without that snippet, I can send and receive all the emails. Subject snippet did work but issue is with only this snippet.

 

Also, note that I can use dynamic first name in the body by Hi <%= recipient.firstName %> but I think only this date one isn't work. I'm not able to understand the issue.

Avatar

Correct answer by
Level 6

Another implementation way

<%
function daysInMonth(today) {
     return new Date(today.getFullYear(), today.getMonth(), 0).getDate();
}
var today = new Date();
%>

<%= daysInMonth(today) %> <%= formatDate (today, "%Bl") %>

 

Avatar

Level 2

Thank you for suggestion Lauren. I will try this way today and let you know whether that worked or not.

Avatar

Level 2

Thank you so much, this snippet worked for me.

Avatar

Level 2

Hi Laurent,

 

I need one more help. Your answer did work. If I want to have month name in French in subject as well as email body. Do you know how to do that?

Avatar

Level 2

Another implementation way - just corrected snippet for month. Found that in JS, month is 0-based so it was creating the issue.

<%
function daysInMonth(today) {
     return new Date(today.getFullYear(), today.getMonth()+1, 0).getDate();
}
var today = new Date();
%>

<%= daysInMonth(today) %> <%= formatDate (today, "%Bl") %>

 

 

Can you help with translation part?

Avatar

Level 6

there are different methods for that, but my prefered one is to create an array in JS:

var monthFr:["Janvier""Février""Mars""Avril""Mai""Juin""Juillet", "Aout", "Septembre", "Octobre","Novembre","Décembre"];

logInfo("Actual French Month: " + monthFr[today.getMonth()] );

ps: this is because "today" as already get declared in the past example