-1

I am currently taking 2 dates from a database and finding the difference between them. I am displaying the value from the sum in my view page but it is currently showing as a decimal. For example, one of the values that gets produced is 1585.913 but I would like that in HH:MM:SS format.

My current code that displays the decimal value is as follows.

Controller

public ActionResult Execution(int id = 0)
    {
        Execution execution = db.Executions.Find(id);

        if (execution == null)
        {
            return HttpNotFound();
        }

        ViewBag.ExecutionSeconds = (execution.End - execution.Start).TotalSeconds;

        return View(execution);

    }

View

@ViewBag.ExecutionSeconds

What changes to this code would I have to make in order for it to display the value in HH:MM:SS format?

Thanks in advance.

D.M
  • 429
  • 1
  • 5
  • 24

3 Answers3

5

You can use .ToString() method to format your TimeSpan. For example :

(execution.End - execution.Start).ToString(@"dd\.hh\:mm\:ss");

More information :

https://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx

Fabjan
  • 13,506
  • 4
  • 25
  • 52
4

Substractions of two dates returns structure of type TimeSpan

(execution.End - execution.Start);// returns TimeSpan
(execution.End - execution.Start).ToString(@"hh\:mm\:ss"); // format it

Timespan formatting

if your time is bigger than 24 hours (as @juharr pointed) you can use

string.Format("{0}:{1}:{2}", (int)time.TotalHours, time.Minutes, time.Seconds);

or include days by replacing formatting string with @"dd\.hh\:mm\:ss"

I'd add an extension for TimeSpan, since such formatting happens pretty often (coppied from here):

public static string ToReadableString(this TimeSpan span)
{
    string formatted = string.Format("{0}{1}{2}{3}",
        span.Duration().Days > 0 ? string.Format("{0:0} day{1}, ", span.Days, span.Days == 1 ? String.Empty : "s") : string.Empty,
        span.Duration().Hours > 0 ? string.Format("{0:0} hour{1}, ", span.Hours, span.Hours == 1 ? String.Empty : "s") : string.Empty,
        span.Duration().Minutes > 0 ? string.Format("{0:0} minute{1}, ", span.Minutes, span.Minutes == 1 ? String.Empty : "s") : string.Empty,
        span.Duration().Seconds > 0 ? string.Format("{0:0} second{1}", span.Seconds, span.Seconds == 1 ? String.Empty : "s") : string.Empty);

    if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);

    if (string.IsNullOrEmpty(formatted)) formatted = "0 seconds";

    return formatted;
}
Community
  • 1
  • 1
Artiom
  • 7,694
  • 3
  • 38
  • 45
  • If the time span is more than a day you will not get the correct value. For example if that time span is 1 day 5 hours and 6 minutes this will output "05:06:00", not "29:06:00". – juharr Aug 06 '15 at 13:32
2

You need to format your TimeSpan which returns execution.End - execution.Start expression with @"hh\:mm\:ss" format like;

TimeSpan ts = execution.End - execution.Start;
string seconds = ts.ToString(@"hh\:mm\:ss"); // 00:26:25

By the way, there is no MM specifier in custom TimeSpan formatting, it should be lowercase as mm specifier.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • If the time span is more than a day you will not get the correct value. For example if that time span is 1 day 5 hours and 6 minutes this will output "05:06:00", not "29:06:00" – juharr Aug 06 '15 at 13:34
  • 1
    What for @Soner was downvoted? – Artiom Aug 06 '15 at 13:40
  • @juharr Again, OP should consider to say this case can be happen or not. It is good of course to consider it in an answer but a downvote should not be here in such a situation that for a subject that OP doesn't _even_ mentioned in my opinion. – Soner Gönül Aug 06 '15 at 13:46