Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TimeSpan的自定义字符串格式_C#_.net_Timespan_Date Format - Fatal编程技术网

C# TimeSpan的自定义字符串格式

C# TimeSpan的自定义字符串格式,c#,.net,timespan,date-format,C#,.net,Timespan,Date Format,我想用C#来格式化时间跨度,方法如下: public static class TimeSpanExtensions { public static string ToDetailedString(this TimeSpan timeSpan) { if (timeSpan == null) throw new ArgumentNullException("timeSpan"); var sb = new StringBu

我想用C#来格式化时间跨度,方法如下:

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";
        }
    }
}
xxx天yyy小时zzz分钟

条件:

  • 额外的秒数应该被截断

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

  • 如果timespan少于一天,我不希望出现“一天”部分。同样,如果跨度小于1小时,我只希望显示分钟部分

  • 是否有任何方法可以使用内置格式字符串完成此操作,或者除了编写自己的函数之外没有其他方法

    编辑:目前正在为此使用我自己的函数。它以分钟为单位作为输入
    (TimeSpan.TotalMinutes)


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


    在.NET 4中,添加了对格式化
    TimeSpan
    的支持,有关详细信息,请参阅。

    TimeSpan在.NET 4.0之前根本没有格式化选项,您必须通过Ticks属性将其转换为DateTime。在DateTime.String(格式)格式选项中没有远程关闭的内容,但您必须自己编写


    在.NET4.0中,TimeSpan获得了一个ToString(格式)覆盖。描述了自定义格式字符串。您的第三个需求将需要代码。

    不幸的是,.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()
    函数中,但它工作得非常好。

    至少在.NET 3.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();
        }
    }
    

    我现在也在使用我自己的方法,它将Timespan(以分钟为单位)作为输入。。。编辑后我的问题中包括了这一点我只是好奇。。。如果天数大于零但小时数为零,会发生什么情况?例如,您是否显示
    XX天,0小时,YY分钟
    ,或者您是否显示
    XX天,YY分钟
    ?您的代码似乎与前者相同。是的,在这种情况下,我希望显示0小时..+1以说明在.NET 4.0之前没有时间跨度格式。我不知道这一点,当我无法在我的.NET2.0项目中使用它时,我会浪费时间试图找出我需要的格式字符串。