.NET Zone is brought to you in partnership with:

I am developer and technology maniac who is working on Microsoft and PHP technologies. I have ASP.NET MVP title and I hold MCAD, MCSD and MCTS certificates. When I have free time I usually play with new technologies, hack something, read books, participate in communities and speak in events. I am also active blogger and my ASP.NET blog is the place you can find some interesting reading about my discoveries and personal thoughts. Gunnar is a DZone MVB and is not an employee of DZone and has posted 105 posts at DZone. You can read more from them at their website. View Full User Profile

ASP.NET MVC: Helper method to display date ranges

01.01.2012
| 2179 views |
  • submit to reddit

I have events web site where I want to show events start and end time to visitors. I wrote simple extension method called DisplayTimeRange() to display event time range on user-friendly manner. My goal is to show times as 01.01.2012 10:00 – 14:00 and 01.01.2012 15:00 – 01.03.2012 18:00. It’s practically displaying time ranges is shortest possible way. In this posting I will show you how to do it using extension method.

For events that happen only on one date I want output like this:

 Event covers only one date

For events that cover more than one date I want output like this:

 Event covers more than one date

Here is my extension method for HtmlHelper. I called it DisplayDateRange and currently it generates output like shown above (styling depends on you).

public static MvcHtmlString DisplayDateRange(this HtmlHelper helper, DateTime from, DateTime to)
{
    var buffer = new StringBuilder(100);
 
    buffer.Append(@"<div class=""dateRange"">");
 
    buffer.Append(from.ToShortDateString());
    buffer.Append(" ");
    buffer.Append(from.ToShortTimeString());
    buffer.Append(" - ");
 
    if (from.Date == to.Date)
    {
        buffer.Append(to.ToShortTimeString());
    }
    else
    {
        buffer.Append(to.ToShortDateString());
        buffer.Append(" ");
        buffer.Append(to.ToShortTimeString());
    }
 
    buffer.Append("</div>");
 
    return new MvcHtmlString(buffer.ToString());
}

This extension method can be easily extended also to nullable DateTime types if you need it.

DateTime formatting may seem not so perfect but it was the only way how to avoid weird long date and time outputs with seconds (we really don’t need seconds here). If you have good idea about how to format dates shorter in code then feel free to drop me a comment here.

References
Published at DZone with permission of Gunnar Peipman, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Kathy John replied on Thu, 2012/02/23 - 9:53am

It is using default formatting based on current culture. You can set it in web.config file, read from browser or set in code. If you want to specify format in view (not sure why you should need it) then you can add additional argument to extension method.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.