'I'd like to change the way the date is formatted in the body of my email to show just month and date using Google's script
I'd like to change the way the date is formatted in the body of my email to show just month and date using Google's script
var email_body = '<html><body> Hey Byro <br> <br> Please see your daily checklist below: <br> <br> <table style = border-collapse:collapse; border = 1 cellpadding = 5><tr>';
for (var row=0;row<data.length;++row){
for (var col = 0; col<data[0].length;++col)
{
if (row==0)
{email_body+='<th>'+data[row][col]+'</th>'}
else{
email_body+='<td>'+data[row][col]+'</td>'
}
}
email_body+='</tr><tr>'
}
email_body += '</tr></table><br><br> Thank You </body></html>'
MailApp.sendEmail(recipiants,subject,"",{htmlBody:email_body})
Solution 1:[1]
There is no date in your template/body of the email.
You can use a standard JS way to get the current date - below I used the new Date().toLocaleDateString() method and I slightly modified the function to cover my own test needs, but the logic stays the same.
function sendDailyTasksReminder() {
var email_body = '<html><body> Hey Byro <br> <br> Please see your daily (<b>' + new Date().toLocaleDateString() + '</b>) checklist below: <br> <br> <table style = border-collapse:collapse; border = 1 cellpadding = 5><tr>';
let checkList = ["Air Filter", "Transmission Fluid", "Replace Spark Plugs"];
checkList.forEach((item, index)=>{
if(index == 0){
email_body+='<th>'+'#'+'</th>'
email_body+='<th>'+'Task'+'</th>'
email_body+='</tr><tr>'
}
email_body+='<td>'+index+'</td>'
email_body+='<td>'+item+'</td>'
email_body+='</tr><tr>'
})
email_body += '</tr></table><br><br> Thank You </body></html>'
MailApp.sendEmail("[email protected]","Test Email Template","",{htmlBody:email_body});
}
To get the date excluding the year:
let dd = new Date()
month = dd.getMonth() + 1;
day = dd.getDate();
And then replace in the template with:
Please see your daily (<b>' + day+'/'+month + '</b>) checklist below ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

