DateTime Format for Blog Posts for BlogEngine.NET
...or wherever you want to standardize output formatting.
Here is a rather simple solution to my issue of Date Time formatting on my blog posts. I noticed that as of a recent version of BlogEngine.NET, the Date looked something like this: "29. June 2008 09:00" I wanted to change it to something more readable to my target readers, some very local, such as: "Sunday, June 29, 2008 09:00 AM". Well, I might want to change it sometime, AND I would like to drop it into multiple Themes, so I decided to place the format in Web.Config AppSettings. I looked up my specific Format and added an appSetting. Then I just replaced the code used in "PostView.ascx" and tested away.
Here's what you can do to implement this yourself.
- Go to .NET Framework Developer's Guide, Custom Date and Time Format Strings and/or locate your format string.
- I used "dddd, MMMM dd, yyyy hh:mm tt"
- Create an appSettings key such as: Custom.DateFormat and assign your format to the value.
- <add key="Custom.DateFormat" value="dddd, MMMM dd, yyyy hh:mm tt" />
- Now add the code to use the format string.
- original: <%=Post.DateCreated.ToString("d. MMMM yyyy HH:mm") %>
This is what it looks like:
<add key="Custom.DateFormat" value="dddd, MMMM dd, yyyy hh:mm tt" /> </appSettings>
<span class="author">by <a href="<%=VirtualPathUtility.ToAbsolute(" mce_href="<%=VirtualPathUtility.ToAbsolute("~/") + "author/" + Post.Author %>.aspx"><%=Post.AuthorProfile != null ? Post.AuthorProfile.DisplayName : Post.Author %></a></span> <span class="pubDate"><%=Post.DateCreated.ToString(ConfigurationSettings.AppSettings["Custom.DateFormat"])%></span>
You can add appSettings directly in Web.config, from IIS Manager, if ASP.NET configuration is available, or from "ASP.NET Web Site Administration Tool". Although this solution was quick and dirty, the best answer may be to role localization into BlogEngine.NET using cultures. This is not a specialty of mine so I would appreciate any suggestions on this topic. It is likely only a few more lines of code for a "quick and dirty" approach but I'd rather make sure to do it right.