字符串模板电子邮件格式化日期 [英] Format date in String Template email
问题描述
我正在使用字符串模板创建一个电子邮件,但是当我打印出一个日期时,它打印出完整的日期(例如,Wed Apr 28 10:51:37 BST 2010)。我想以dd / mm / yyyy格式打印出来,但不知道如何在.st文件中格式化。
I'm creating an email using String Template but when I print out a date, it prints out the full date (eg. Wed Apr 28 10:51:37 BST 2010). I'd like to print it out in the format dd/mm/yyyy but don't know how to format this in the .st file.
我不能修改日期(使用java的simpleDateFormatter),因为我迭代一个包含日期的对象集合。
I can't modify the date individually (using java's simpleDateFormatter) because I iterate over a collection of objects with dates.
有没有办法在.st电子邮件模板中格式化日期?
Is there a way to format the date in the .st email template?
推荐答案
使用其他渲染器:
internal class AdvancedDateTimeRenderer : IAttributeRenderer
{
public string ToString(object o)
{
return ToString(o, null);
}
public string ToString(object o, string formatName)
{
if (o == null)
return null;
if (string.IsNullOrEmpty(formatName))
return o.ToString();
DateTime dt = Convert.ToDateTime(o);
return string.Format("{0:" + formatName + "}", dt);
}
}
然后将其添加到您的StringTemplate,如: / p>
and then add this to your StringTemplate such as:
var stg = new StringTemplateGroup("Templates", path);
stg.RegisterAttributeRenderer(typeof(DateTime), new AdvancedDateTimeRenderer());
然后在st文件中:
$YourDateVariable; format="dd/mm/yyyy"$
它应该工作
这篇关于字符串模板电子邮件格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!