我想将时间设置为几分钟,只有几秒钟,有人知道这段代码有什么问题吗? (时间mm:ss,hhss,hh。示例01:12,1072,10秒)

public double timeToSeconds(string TimeToSplit)
{
    string[] Timesplitter = new string[2];
    double minutes;
    double seconds;
    Timesplitter = TimeToSplit.Split(':');

    minutes = double.Parse(Timesplitter[0]);        //double with minutes
    seconds = double.Parse(Timesplitter[1]);        //double with seconds
    if (minutes != 0)
    {
        seconds = seconds + (minutes * 60);
    }
    return seconds;
}

最佳答案

您应该将字符串解析为TimeSpan:

CultureInfo provider = CultureInfo.InvariantCulture;
TimeSpan ts = TimeSpan.ParseExact(TimeToSplit, "mm\\:ss\\,ff", provider);

return ts.TotalSeconds

关于c# - 我想将我的分钟,几秒钟和几百个拆分为几秒钟和几百个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16608638/

10-12 22:17