【发布时间】:2017-07-03 05:34:38
【问题描述】:
我不知道这种格式在编程语言中称为2d30m 是什么。但是,我看到一些 Jquery 插件或 Youtube 使用这种时间格式跳转到时间 url,例如 &t=3m11s。因为我不知道 excat 关键字,所以很难 google。
所以,我想使用这种格式并在 C# 中翻译成TimeSpan 对象。我怎样才能做到这一点?
现在我正在尝试通过此代码从字符串中提取值
public static void Main()
{
String str = "2d30m";
int day = 0, minute = 0;
//Get Day
day = Helper(str, "d");
//Get Minute
minute = Helper(str, "m");
//Create timespan
var myTimeSpan = new TimeSpan(days: day, hours: 0, minutes: minute, seconds: 0);
Console.Write(myTimeSpan);
}
public static int Helper(string input, string timeCode)
{
int output = 0;
int indexOf = input.LastIndexOf(timeCode, StringComparison.OrdinalIgnoreCase);
if (indexOf > 0)
{
string strTime = input.Substring(Math.Max(0, indexOf - 2), 2);
Console.WriteLine(strTime);
strTime = System.Text.RegularExpressions.Regex.Replace(strTime, "[^0-9.]", ""); // remove all alphabet
output = Convert.ToInt32(strTime);
}
return output;
}
【问题讨论】:
标签: c# asp.net .net datetime timespan