从"jira notation"中的时间字符串中提取分钟

use*_*364 5 c# timespan jira

我试图从"jira time notation"输入的用户输入中提取分钟数.

例如,我想实现以下结果

  • 输入:"30m"/输出:30
  • 输入:"1h 20m"/输出:80
  • 输入:"3h"/输出180

从研究中,我发现TimeSpan.ParseExact,但我无法弄清楚如何使用它来实现我的需要.

非常感谢所有的帮助.

我的代码到目前为止:

public static int TextToMins(string TimeText)
    {
        CultureInfo culture = new CultureInfo("en-IE");
        string[] formats = { "What goes here?" };
        TimeSpan ts = TimeSpan.ParseExact(TimeText.Trim().ToLower(), formats, culture);
        return Convert.ToInt32(ts.TotalMinutes);
    }
Run Code Online (Sandbox Code Playgroud)

Dav*_*rno 2

有点大锤的方法,但是怎么样:

public static int TextToMins(string timeText)
{
    var total = 0;
    foreach (var part in timeText.Split(' '))
    {
        if (part[part.Length - 1] == 'h')
        {
            total += 60 * int.Parse(part.Trim('h'));
        }
        else
        {
            total += int.Parse(part.Trim('m'));
        }
    }
    return total;
}
Run Code Online (Sandbox Code Playgroud)