很抱歉,由于我的英语不好,这个标题让你感到困惑。 我有一个ListBox,其中包含许多具有时间格式的项目 (例如:00:02:22:33) 我想将这种时间格式转换为分钟
X = inputdlg ('Please enter your password');
x = str2double (X {1,1} );
password = 1111; %whatever
if (x == password)
uiwait( msgbox('Welcome!') );
else
locked_flag = false
counter = 1
while (x ~= 1111)
if (x == password)
uiwait( msgbox('Welcome!') );
break;
else
uiwait( errordlg('Try Again!!') );
counter = counter + 1
if (counter == 3)
locked_flag = true;
%show 'locked' dialog of some kind here
break;
end
X = inputdlg('Please enter your password');
x = str2double ( X{1,1} );
end
end
end
%can now check if locked_flag true to start unlock logic or other...
我正在尝试编写的方法如下:
For example: 00:02:22:33 -> 02 hours = 120 minutes
33 seconds = 33/60 = 0.55 minutes
So result is 120+22+0.55 = 142.55
我是C#的新手,所以我尽可能详细解释,所以请帮助我:(
答案 0 :(得分:0)
将字符串解析为时间跨度并使用TotalMinutes
属性。
var time = TimeSpan.Parse("00:02:22:33");
var convertedToMinutes = time.TotalMinutes; //Returns 142.55
这将更新您的列表项
for (int i = 0; i < listBox1.Items.Count; i++)
{
TimeSpan time = TimeSpan.Parse(listBox1.Items[i].ToString());
listBox1.Items[i] = time.TotalMinutes;
}
或者,TryParse()
可用于处理格式不正确的字符串:if (TimeSpan.TryParse(listBox1.Items[i].ToString(), out time)) { listBox1.Items[i] = time.TotalMinutes; }
答案 1 :(得分:0)
您可以尝试以下操作:
var regex = new System.Text.RegularExpressions.Regex(@"00:\d{2}:\d{2}:\d{2}");
foreach (var item in l.Items)
{
if (regex.IsMatch(item))
{
item = TimeSpan.Parse(item).TotalMinutes.ToString();
}
}