Skip to content

Calculating the number of hours, minutes and seconds in C# (Part 2)

February 22, 2012

As I had previously calculated up to hours on the previous solution, it seemed pretty cumbersome if the hours flowed into days, and from then you can get tens and tens of hours that can be hard to breakdown, so the obvious answer is to place it into days.

Here’s a better solution that calculates the number of days based on a timespan of seconds as the input: –

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DateTimeHelpers
/// </summary>
public class DateTimeHelpers
{
public static string VerbaliseTimespan(TimeSpan timeSpan)
{
string timeSpanString = “”;

if (timeSpan.Days > 0)
{
if (timeSpan.Days == 1)
timeSpanString += “1 day”;
else
timeSpanString += timeSpan.Days + ” days”;
}

if (timeSpan.Hours > 0)
{
if (timeSpanString.Length > 0)
timeSpanString += “, “;

if (timeSpan.Hours == 1)
timeSpanString += “1 hour”;
else
timeSpanString += timeSpan.Hours + ” hours”;
}

if (timeSpan.Minutes > 0)
{
if (timeSpanString.Length > 0)
timeSpanString += “, “;

if (timeSpan.Minutes == 1)
timeSpanString += “1 minute”;
else
timeSpanString += timeSpan.Minutes + ” minutes”;
}

if (timeSpan.Seconds > 0)
{
if (timeSpanString.Length > 0)
timeSpanString += ” and “;

if (timeSpan.Seconds == 1)
timeSpanString += “1 second”;
else
timeSpanString += timeSpan.Seconds + ” seconds”;
}

return timeSpanString;
}

public static string VerbaliseNumOfSeconds(long seconds, int significantFactors, Boolean shortMode)
{
int currentSignificantFactors = 0;

string[] parts = shortMode
? new string[] { “day”, “hr”, “min”, “sec” }
: new string[] { “day”, “hour”, “minute”, “second” };

string timeSpanString = “”;

TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);

if (timeSpan.Days > 0 && currentSignificantFactors < significantFactors)
{
if (timeSpan.Days == 1)
timeSpanString += “1 ” + parts[0];
else
timeSpanString += timeSpan.Days + ” ” + parts[0] + “s”;

currentSignificantFactors++;
}

if (timeSpan.Hours > 0 && currentSignificantFactors < significantFactors)
{
if (timeSpanString.Length > 0)
{
if (!shortMode && currentSignificantFactors == significantFactors – 1)
timeSpanString += “and “;
else
timeSpanString += “, “;
}

if (timeSpan.Hours == 1)
timeSpanString += “1 ” + parts[1];
else
timeSpanString += timeSpan.Hours + ” ” + parts[1] + “s”;

currentSignificantFactors++;
}

if (timeSpan.Minutes > 0 && currentSignificantFactors < significantFactors)
{
if (!shortMode && currentSignificantFactors == significantFactors – 1)
timeSpanString += “and “;
else
timeSpanString += “, “;

if (timeSpan.Minutes == 1)
timeSpanString += “1 ” + parts[2];
else
timeSpanString += timeSpan.Minutes + ” ” + parts[2] + “s”;

currentSignificantFactors++;
}

if (timeSpan.Seconds > 0 && currentSignificantFactors < significantFactors)
{
if (!shortMode)
timeSpanString += “and “;
else
timeSpanString += “, “;

if (timeSpan.Seconds == 1)
timeSpanString += “1 ” + parts[3];
else
timeSpanString += timeSpan.Seconds + ” ” + parts[3] + “s”;
}

return timeSpanString;

}

public static string EnglishLongDate(object dateTime)
{
return String.Format(“{0:HH:mm}, {0:ddd dd MMMM yyyy}”, dateTime);
}
}

 

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: