【问题标题】:Using timespan for values above 60 minutes对 60 分钟以上的值使用时间跨度
【发布时间】:2016-09-30 04:24:17
【问题描述】:

我在 C# 中使用时间跨度将十进制转换为时间

TimeSpan.FromMinutes(59.42).ToString("mm\:ss");输出 59:25。但是,例如,我想处理 60 分钟以上的时间:

TimeSpan.FromMinutes(62.87).ToString("mm\:ss") 我想输出为 62:52 但我得到的是 02.52。

.Net 有没有办法处理这个问题?

【问题讨论】:

    标签: formatting timespan


    【解决方案1】:

    您正在创建所需的 TimeSpan,只是没有正确显示它。

    格式字符串中的“mm”项给出了 Minutes 属性(始终在 0-59 范围内)而不是 TotalMinutes 属性(TimeSpan 中的总分钟数)。

    TimeSpan ts = TimeSpan.FromMinutes(62.8);
    string output = string.Format("{0}:{1}", 
         // Display the total minutes, throwing away the fractional portion.
         (int) ts.TotalMinutes, 
         // Display the seconds component (0-59) as two digits with a leading
         // zero if necessary.
         ts.Seconds.ToString("D2"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 2012-06-16
      • 1970-01-01
      • 2012-10-19
      • 2022-10-13
      相关资源
      最近更新 更多