DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
  • A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
  • How To Backup and Restore a PostgreSQL Database
  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  1. DZone
  2. Coding
  3. Frameworks
  4. ASP.NET MVC: Helper method to display date ranges

ASP.NET MVC: Helper method to display date ranges

Gunnar Peipman user avatar by
Gunnar Peipman
·
Jan. 01, 12 · News
Like (0)
Save
Tweet
Share
4.82K Views

Join the DZone community and get the full member experience.

Join For Free

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:

DisplayDateRange(): Event covers only one date

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

DisplayDateRange(): 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.

ASP.NET

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
  • A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
  • How To Backup and Restore a PostgreSQL Database
  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: