我需要使用用户提供的格式将 TimeSpan 解析为字符串
我尝试了很多选项,包括各种 RegEx.Replace 和 Parse 选项。
在下面,我为 TimeSpan 添加了一个扩展方法。
public void should_return_hours_and_minutes_and_seconds()
{
_ts = new TimeSpan(2, 1, 30, 10);
var format = "HH:mm:ss"; // Input by User. Goal is to include other formats
var returnedVal = _ts.ToString(format);
Assert.That(returnedVal, Is.EqualTo("49:30:10")); // Days rollup to hours
}
最佳答案
尝试使用这段代码
public void should_return_hours_and_minutes_and_seconds()
{
DateTime d = new DateTime(time_span.Ticks);
string time = d.ToString("HH:mm:ss");
}
关于c# - 将 TimeSpan 解析或转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005990/