C# 以MM:SS格式添加两次,并以字符串形式返回到最大精度

C# 以MM:SS格式添加两次,并以字符串形式返回到最大精度,c#,.net,string,add,addition,C#,.net,String,Add,Addition,我想写一个函数,它添加两个字符串,一个是MM:ss格式,另一个是秒数,然后进行加法,注意额外的秒数,以相同的格式以最高的精度返回它们。 //例:1:30+65=2:35 我已经试过了,但我认为它可以变得更通用,请看代码 public static string maxLevel(string s1, string s2) { string[] parts1 = null; string[] parts2 = null;

我想写一个函数,它添加两个字符串,一个是MM:ss格式,另一个是秒数,然后进行加法,注意额外的秒数,以相同的格式以最高的精度返回它们。 //例:1:30+65=2:35

我已经试过了,但我认为它可以变得更通用,请看代码

public static string maxLevel(string s1, string s2)
        {
            string[] parts1 = null;
            string[] parts2 = null;

            if (s1.Contains(":"))
            {
                parts1 = s1.Split(':');
            }

            if (s2.Contains(":"))
            {
                parts2 = s2.Split(':');
            }


            int minutes1 = 0;
            int seconds1 = 0;
            int minutes2;
            int seconds2 = 0;
            int newSeconds = 0;
            int newMinutes = 0;
            int carry = 0;

            if (parts1 != null && parts1.Length > 1)
            {
                minutes1 = Convert.ToInt32(parts1[0]);
               // int HoursInminutes = (12 + (minutes % 60)) * 60;
                seconds1 = Convert.ToInt32(parts1[1]);
            }

            else
            {
                seconds1 = Convert.ToInt32(s1);
                int minutes = seconds1 / 60;
                if (minutes >= 1)
                {
                    carry = seconds1 - 60;
                    //newSeconds = seconds2 + carry;
                }

                newMinutes = minutes;
                newSeconds = carry;
            }

            if (parts2 != null && parts2.Length > 1)
            {
                minutes2 = Convert.ToInt32(parts2[0]);
                seconds2 = Convert.ToInt32(parts2[1]);
                //newMinutes = minutes2;
                newMinutes += minutes2;
                newSeconds += seconds2;
            } 


            else
            {
                seconds2 = Convert.ToInt32(s2);
                int minutes = seconds2/60;
                if(minutes >= 1)
                {
                    carry = seconds2 - 60;
                    newSeconds = seconds1 + carry;
                    newMinutes = minutes1 + minutes;
                }
                else
                {
                    carry = seconds2 - seconds1;
                    newSeconds = carry;
                    newMinutes = 1 + minutes1;
                }


            }

            return newMinutes + ":" + newSeconds;

        }
当我把s1改为55秒,s2改为1:30,那么这个代码就不起作用了。
我认为它需要一些修改,是否有人可以帮助我或用C告诉我正确的方法,因为你想让用户指定超过59秒和/或分钟,我认为TimeSpan.TryParseExact不起作用,因为你不能向它传递超过59秒或分钟的时间。这些字段必须为0-59

但是,您可以编写自己的自定义解析器来拆分冒号字符上的输入字符串:然后使用int.TryParse尝试将结果部分解析为整数,然后使用TimeSpan.FromSeconds传递任意秒数以创建新的TimeSpan对象,您可以使用.Add方法添加从分钟部分创建的另一个时间跨度(如果已指定)

首先,我们可以编写一个方法,从[integer]或[integer]:[integer]格式的字符串返回时间跨度:

public static TimeSpan CustomParse(string input)
{
    // Split the string on the ':' character
    var parts = input?.Split(':');

    // Make sure we have something to work with
    if (parts == null || parts.Length == 0) 
        throw new FormatException("input format must be \"%m:%s\" or \"%s\"");

    int seconds;

    // Only a single number represents seconds
    if (parts.Length == 1)
    {
        if (int.TryParse(parts[0], out seconds))
        {
            return TimeSpan.FromSeconds(seconds);
        }
    }
    // Otherwise the first number is minutes and the second one is seconds
    else
    {
        int minutes;
        if (int.TryParse(parts[0], out minutes) &&
            int.TryParse(parts[1], out seconds))
        {
            return TimeSpan.FromSeconds(seconds).Add(TimeSpan.FromMinutes(minutes));
        }
    }

    // If we haven't returned anything yet, there was an error in the format
    throw new FormatException("input format must be \"%m:%s\" or \"%s\"");
}
然后我们可以编写另一个函数,接收两个字符串,使用上面的方法将它们转换为时间跨度,并返回将它们作为字符串相加的结果:

public static string Add(string s1, string s2)
{
    return CustomParse(s1).Add(CustomParse(s2)).ToString("%m\\:%s");
}
现在,我们可以使用您的示例字符串对此进行测试:

private static void Main()
{
    string first = "1:30";
    string second = "65";
    string result = Add(first, second);

    Console.WriteLine($"{first} + {second} = {result}");

    GetKeyFromUser("\nDone! Press any key to exit...");
}
输出


该类型用于处理时间,并具有Parse、TryParse和Subtract方法says以MM:ss格式添加2个字符串。然后继续显示示例1:30+65=2:35,其中添加的两个字符串中只有一个是mm:ss格式。。。沃特?还有,像51:20+56:12这样的东西呢?是的,一个字符串以MM:SS表示,另一个字符串仅以secods 65表示,所以在添加时我们要注意->1:30+65=2:35回答得好!非常感谢。