Reputation: 113
I have a textbox that has this text 25:00:00. This represents 25 hours. I am trying to convert this text to a timespan with this code:
TimeSpan tTime = TimeSpan.Parse(man_hours_nbr.Text);
double sum10 = tTime.TotalHours;
hours = Convert.ToDecimal(sum10);
The problem is that tTime is seeing 25 days (600 hours) instead of 25 hours. Why is it reading 25 days?? I then need the timespan as a decimal so it can be displayed as such. What am I doing wrong? Thanks
Upvotes: 2
Views: 1701
Reputation: 56
The TimeSpan.ParseExact takes the clock time and not the amount of time. Hence, you cannot use it for your requirement. From what I understand, you want to get the total number of hours from a specified string with the format "hours:minutes:seconds". Hope the code below helps you solve the problem. There have been answers posted already. The following code is something I would have done if faced with the problem.
This line in your code
var hours = GetHours(man_hours_nbr.Text);
Here's the GetHours method
public static double GetHours(string timeString)
{
var splitTime = timeString.Split(':');
if (splitTime.Length != 3)
throw new ArgumentException("Time string not in format HH:MM:SS");
var hours = Convert.ToDouble(splitTime[0]);
var mins = Convert.ToDouble(splitTime[1]);
var seconds = Convert.ToDouble(splitTime[2]);
var time = new TimeSpanBuilder()
.WithHours(hours)
.WithMinutes(mins)
.WithSeconds(seconds)
.Build();
return time.TotalHours;
}
Here is the TimeSpanBuilder class, (uses the builder pattern)
public class TimeSpanBuilder
{
private TimeSpan _ts = TimeSpan.Zero;
public TimeSpanBuilder WithHours(double hours)
{
_ts = _ts.Add(TimeSpan.FromHours(hours));
return this;
}
public TimeSpanBuilder WithMinutes(double mins)
{
_ts = _ts.Add(TimeSpan.FromMinutes(mins));
return this;
}
public TimeSpanBuilder WithSeconds(double seconds)
{
_ts = _ts.Add(TimeSpan.FromMinutes(seconds));
return this;
}
public TimeSpan Build()
{
return _ts;
}
}
Upvotes: 0
Reputation: 11
The TimeSpan struct has limits to the values that can be used Christian. You could do this (shown below) instead if you are only capturing man hours in your text box. For simplicity I am omitting the obvious error checks like max min values and nulls.
var textBoxValue = "25:20:00";
var hours = int.Parse(textBoxValue.Split(':')[0]) + int.Parse(textBoxValue.Split(':')[1])/60d + int.Parse(textBoxValue.Split(':')[2]) / 3600d;
Upvotes: 1
Reputation: 10940
You can do it like this:
string[] splitTime = man_hours_nbr.Text.Split(':');
int hours = Int32.Parse(splitTime[0]);
int minutes = Int32.Parse(splitTime[1]);
int seconds = Int32.Parse(splitTime[2]);
double totalHours = hours + minutes / 60.0 + seconds / 3600.0;
I simply split the text by a colon, into it's parts. Then parse the values and adds them together at the end.
Upvotes: 3
Reputation: 7960
As some pointed out in the comments, you can't directly parse 25 hours. But what you can do is parse the hours to days and then get the total hours from the TimeSpan. Along the lines like so:
TimeSpan ts = TimeSpan.ParseExact(stringValue, @"dd\:hh\:mm\:ss", null);
double hours = ts.TotalHours;
The @"dd..." string is the format of the input where "d" stands for day, "h" for hour, "m" for minute and "s" for second. So your input would be "01:01:00:00" for 1 day and 1 hour (25 hours in total)
Upvotes: -1