将TimeSpan大于23:59:59划分为int值
问题描述:
我该怎么做? :(我希望TS的格式为00:00:00
How can I do this? :( I want TS be in this format "00:00:00"
TimeSpan TS =TimeSpan.Parse("140:43") /30;
答
试试这个:
Try this:
string input = "140:43";
string[] parts = input.Split(':');
int hours = int.Parse(parts[0]);
int minutes = int.Parse(parts[1]);
int seconds;
if (parts.Length < 3)
{
seconds = 0;
}
else
{
seconds = int.Parse(parts[2]);
}
TimeSpan TS = new TimeSpan(hours, minutes, seconds);
TimeSpan resultAfterDivision = new TimeSpan(TS.Ticks / 30);
首先,您需要拆分给定的字符串。第一部分是小时数,第二部分是分钟数。如果没有给出第三部分,则秒数为0.否则,秒数是输入的第三部分。
然后,分割 TimeSpan
,你需要划分它的滴答数。
First, you need to split the given string. The first part is the number of hours and the second part the number of minutes. If no third part is given, the number of seconds is 0. Otherwise, the number of seconds is the third part of the input.
Then, to divide the TimeSpan
, you need to divide its number of ticks.