ASP.NET accumulated time in seconds

There is a demand, the character requires hh: mm: ss format of the time in seconds accumulation time, similar to a timer, regardless of the accumulated time with date, as follows:

/// <summary>
/// 按秒累加时间
/// </summary>
/// <param name="time">格式必须为 hh:mm:ss </param>
/// <param name="number">要累加的秒数</param>
/// <returns></returns>
public string AddTimeBySeconds(string time, int number)
{
	string val = time;

	int mTime = 0;    //分钟数
	int sTime = 0;    //秒数

	string[] ArrTime = time.Split(':');
	if (ArrTime.Length == 3 && number > 0)
	{
		int hour = Convert.ToInt32(ArrTime[0]);    //时
		int minute = Convert.ToInt32(ArrTime[1]);  //分
		int second = Convert.ToInt32(ArrTime[2]);  //秒

		sTime = second + number;

		if (sTime > 59)
		{
			second = sTime % 60;
			mTime = minute + sTime / 60;

			if (mTime > 59)
			{
				minute = mTime % 60;
				hour = hour + mTime / 60;
			}
			else
			{
				minute = mTime;
			}
		}
		else
		{
			second = sTime;
		}

		val = string.Format("{0}:{1}:{2}",
			(hour < 10 ? ("0" + hour) : hour.ToString()),
			(minute < 10 ? ("0" + minute) : minute.ToString()),
			(second < 10 ? ("0" + second) : second.ToString()));

	}

	return val;
}

E.g:

AddTimeBySeconds ( "07:13:13", 50) -> get: 07: 14:03

AddTimeBySeconds ( "24:59:59", 125) -> get: 25: 02: 04

Guess you like

Origin blog.csdn.net/qq_24470501/article/details/90402901