【问题标题】:Convert a decimal value to time format in c# [duplicate]在c#中将十进制值转换为时间格式[重复]
【发布时间】:2023-03-04 14:56:01
【问题描述】:

我目前正在从数据库中获取 2 个日期并找出它们之间的差异。我在我的视图页面中显示总和的值,但它当前显示为小数。例如,生成的值之一是 1585.913,但我希望采用 HH:MM:SS 格式。

我当前显示十进制值的代码如下。

控制器

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);

    }

查看

@ViewBag.ExecutionSeconds

我必须对此代码进行哪些更改才能以 HH:MM:SS 格式显示值?

提前致谢。

【问题讨论】:

  • 1585.913 是第二个吗?
  • 请更清楚地解释您希望如何完成“转换”。 1585.913 应该以秒为单位吗?添加具有预期输出的示例将非常有帮助。
  • 是的,以秒为单位。所以我期待 00:26:26

标签: c#


【解决方案1】:

您可以使用.ToString() 方法来格式化您的TimeSpan。例如:

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

更多信息:

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

【讨论】:

    【解决方案2】:

    两个日期的减法返回TimeSpan类型的结构

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

    Timespan formatting

    如果您的时间超过 24 小时(正如 @juharr 指出的那样),您可以使用

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

    或通过将格式字符串替换为 @"dd\.hh\:mm\:ss" 来包含天数

    我会为 TimeSpan 添加一个扩展,因为这种格式经常发生(复制自 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;
    }
    

    【讨论】:

    • 如果时间跨度超过一天,您将无法获得正确的值。例如,如果该时间跨度是 1 天 5 小时 6 分钟,这将输出“05:06:00”,而不是“29:06:00”。
    【解决方案3】:

    您需要格式化您的TimeSpan,它返回execution.End - execution.Start 表达式,格式为@"hh\:mm\:ss"

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

    顺便说一句,custom TimeSpan formatting 中没有MM 说明符,应该是小写的mm specifier

    【讨论】:

    • 如果时间跨度超过一天,您将无法获得正确的值。例如,如果该时间跨度是 1 天 5 小时 6 分钟,这将输出“05:06:00”,而不是“29:06:00”
    • @Soner 被否决了怎么办?
    • @juharr 同样,OP 应该考虑说这种情况是否会发生。在回答中考虑它当然很好,但在我认为 OP 没有甚至提到的主题的情况下,不应该在这里投反对票。
    猜你喜欢
    • 1970-01-01
    • 2014-06-25
    • 2010-10-15
    • 2017-09-03
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多