# Calculate Total Hours and Minutes from string variable using C# and VB.Net in ASP.Net

## Last Reply one year ago By pandeyism

Posted one year ago

hello,

i am using timespan to calculate time, it works fine but if timespan1 or timespan2 has value like 24:17  or 25:60 it gives erorr that it is out of range

or

i can just replace if 24:17  so if hour is 24 and min is more than 00 so it replaces min with 00

```string timeStamp1 = item.Time.Replace(" hours ", ":").Replace("min", "").Replace("hour", ":").Replace("min", "");
string timeStamp2 = lblholdingtime.Text.Replace(" hours ", ":").Replace("min", "").Replace("hour",":").Replace("min","");
ts1 = TimeSpan.Parse(timeStamp1);
ts2 = TimeSpan.Parse(timeStamp2);```

```int hours = 00;
int min = 00;
hours += ts1.Hours;
min +=ts1.Minutes;```

Posted one year ago

Hi nauna,

Refer below sample.

First you need to calculate total minutes from what time data you have. And then add both calulated total minute.

Code

C#

```protected void Page_Load(object sender, EventArgs e)
{
item.Text = "24:17".Replace(" hours ", ":").Replace("min", "").Replace("hour", ":").Replace("min", "");
// Calculate total minutes.
int itemTotalMinutes = (Convert.ToInt32(item.Text.Split(':')[0]) * 60) + Convert.ToInt32(item.Text.Split(':')[1]);
lblholdingtime.Text = "25:59".Replace(" hours ", ":").Replace("min", "").Replace("hour", ":").Replace("min", "");
// Calculate total minutes.
int holdingtimTotalMinutes = (Convert.ToInt32(lblholdingtime.Text.Split(':')[0]) * 60) + Convert.ToInt32(lblholdingtime.Text.Split(':')[1]);
// Calculate total minutes from both.
int totalMinute = itemTotalMinutes + holdingtimTotalMinutes;
// Convertint to Hours and minutes.
Response.Write((totalMinute / 60) + " Hours : " + (totalMinute % 60) + " minutes");
}```

VB.Net

```Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
item.Text = "24:17".Replace(" hours ", ":").Replace("min", "").Replace("hour", ":").Replace("min", "")
Dim itemTotalMinutes As Integer = (Convert.ToInt32(item.Text.Split(":"c)(0)) * 60) + Convert.ToInt32(item.Text.Split(":"c)(1))
lblholdingtime.Text = "25:59".Replace(" hours ", ":").Replace("min", "").Replace("hour", ":").Replace("min", "")
Dim holdingtimTotalMinutes As Integer = (Convert.ToInt32(lblholdingtime.Text.Split(":"c)(0)) * 60) + Convert.ToInt32(lblholdingtime.Text.Split(":"c)(1))
Dim totalMinute As Integer = itemTotalMinutes + holdingtimTotalMinutes
Response.Write((totalMinute / 60) & " Hours : " & (totalMinute Mod 60) & " minutes")
End Sub```

Output

50 Hours : 16 minutes