代码之家  ›  专栏  ›  技术社区  ›  Hari Menon

TimeSpan的自定义字符串格式

  •  4
  • Hari Menon  · 技术社区  · 15 年前

    我想用C#来格式化时间跨度:

    xxx天yyy小时zzz分钟

    1. 额外的秒数应该被截断

    2. 天是我想要的最大单位。我希望34天显示为34天,而不是1个月4天等。

    有没有什么方法可以使用内置格式字符串来实现这一点,或者除了编写自己的函数之外没有其他方法?

    (TimeSpan.TotalMinutes) :

    private static string GetTimeStringFromMinutes(double p)
            {
                var minutes = (int) p;
                int hours = minutes / 60;
                minutes = minutes % 60;
                int days = hours/24;
                hours = hours%24;
                string dayPart = days + " day(s) ";
                string hoursPart = hours + " hour(s) ";
                string minutesPart = minutes + " minute(s)";
                if (days != 0)
                    return (dayPart + hoursPart + minutesPart);
                if (hours != 0)
                    return (hoursPart + minutesPart);
                return (minutesPart);
            }
    
    4 回复  |  直到 15 年前
        1
  •  7
  •   Hans Passant    15 年前

    在.NET4.0中,TimeSpan获得了一个ToString(格式)重写。描述了自定义格式字符串 here . 你的第三个要求是需要代码。

        2
  •  5
  •   Richard    15 年前

    在.NET3.5及更早版本中,您需要编写自己的函数。

    在.NET4中,添加了对格式的支持 TimeSpan ,请参见 TimeSpan.ToString(string) 详情。

        3
  •  3
  •   Alex Essilfie    15 年前

    至少在.NET3.5中没有任何内置的方法来满足您的需求。下面是一个扩展 TimeSpan 提供您想要的功能。

    public static class TimeSpanEx
    {
        public static string FormattedString(this TimeSpan ts)
        {
            int days = (int)ts.TotalDays;
            int hrs = (int)ts.Hours;
            int mins = (int)ts.Minutes;
            StringBuilder sb = new StringBuilder();
    
            if (days > 0)
            {
                sb.Append(days.ToString() + (days == 1 ? " day, " : " days, "));
            }
    
            if (hrs > 0 || days > 0)
            {
                sb.Append(hrs.ToString() + (hrs == 1 ? " hour, " : " hours, "));
            }
    
            sb.Append(mins.ToString() + (mins == 1 ? " min" : " mins"));
    
            return sb.ToString();
        }
    }
    
        4
  •  2
  •   Oliver    15 年前

    不幸的是,在.Net中没有直接可用的内容。就我自己而言,我是这样解决问题的:

    public static class TimeSpanExtensions
    {
        public static string ToDetailedString(this TimeSpan timeSpan)
        {
            if (timeSpan == null)
                throw new ArgumentNullException("timeSpan");
    
            var sb = new StringBuilder(30);
    
            var current = timeSpan.ToDaysString();
    
            if (!String.IsNullOrEmpty(current))
                sb.Append(current);
    
            current = timeSpan.ToHoursString();
    
            if (!String.IsNullOrEmpty(current))
            {
                if (sb.Length > 0)
                    sb.Append(" ");
    
                sb.Append(current);
            }
    
            current = timeSpan.ToMinutesString();
    
            if (!String.IsNullOrEmpty(current))
            {
                if (sb.Length > 0)
                    sb.Append(" ");
    
                sb.Append(current);
            }
    
            return sb.ToString();
        }
    
        public static string ToDaysString(this TimeSpan timeSpan)
        {
            if (timeSpan == null)
                throw new ArgumentNullException("timeSpan");
    
            int days = (int)timeSpan.TotalDays;
    
            switch (days)
            {
                case 0:
                    return String.Empty;
                case 1:
                    return "1 day";
                default:
                    return days + " days";
            }
        }
    
        public static string ToHoursString(this TimeSpan timeSpan)
        {
            if (timeSpan == null)
                throw new ArgumentNullException("timeSpan");
    
            switch (timeSpan.Hours)
            {
                case 0:
                    return String.Empty;
                case 1:
                    return "1 hour";
                default:
                    return timeSpan.Hours + " hours";
            }
        }
    
        public static string ToMinutesString(this TimeSpan timeSpan)
        {
            if (timeSpan == null)
                throw new ArgumentNullException("timeSpan");
    
            switch (timeSpan.Minutes)
            {
                case 0:
                    return String.Empty;
                case 1:
                    return "1 minute";
                default:
                    return timeSpan.Minutes + " minutes";
            }
        }
    }
    

    ToDetailedString()