TimeSpan conversion issue (giving days instead of hours)

convert string to timespan c#
convert datetime to timespan c#
c# convert string am/pm to timespan
convert timespan to 12 hour format in c#
timespan parseexact
string was not recognized as a valid timespan
convert string to time c#
c# datetime

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

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.

TimeSpan.Parse Method (System), At least one of the days, hours, minutes, or seconds components is outside its valid range. Examples. The following example uses the Parse method to convert​  A TimeSpan value can be represented as [ -] d. hh: mm: ss. ff, where the optional minus sign indicates a negative time interval, the d component is days, hh is hours as measured on a 24-hour clock, mm is minutes, ss is seconds, and ff is fractions of a second. The value of the Days property is the day component, d.

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;

C# TimeSpan Examples, The code creates a TimeSpan with 1 day, 2 hours, and 30 seconds. Double: These methods convert a number of double type into a TimeSpan struct instance. Remarks. A TimeSpan value can be represented as [ -] d. hh: mm: ss. ff, where the optional minus sign indicates a negative time interval, the d component is days, hh is hours as measured on a 24-hour clock, mm is minutes, ss is seconds, and ff is fractions of a second. The value of the Hours property is the hours component, hh.

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()

    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;

Fundamentals of Crime Mapping, To convert minutes into decimals, we need to divide by 60, so the formula we want in units typically still work an 8- or 10-hour day, and we need to give them a timespan that gives them the best likelihood of catching the offender in the act. You can find the total hour as below code. double hours = timeSpan.Hours + (timeSpan.Days * 24); And this will also give you total hours. int totalHours = (int)timeSpan.TotalHours; This will suppress the minutes and give you only hours.

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)

Date and time in C# - working with date and time in CSharp, The DateTime value type represents dates and times with values ranging TimeSpan represents a time interval (duration of time or elapsed time) that is number of days, hours, minutes, seconds, and fractions of a second. I get you can convert a timespan into ticks, days, hours. minutes, seconds ect. with SKstuff but only works when you convert a timespan into a single hour or minute.. This does not work Set {_test} to "86400 Seconds" parsed as timespan to converted days, hours, minutes and seconds Just returns 1 day, 1 hour, 1 minute, 1 second

Duration class - dart:core library, For example, a duration of 2 days is always 48 hours, even when it is added to a To create a new Duration object, use this class's single constructor giving the comparison operators, plus a set of constants useful for converting time units. Hi all, I am having the following issue: While my Do Until settings are configured as follows: Is this a bug or could someone tell me what I did wrong? The flow run failed the same day it was launched. Thx, Glenda

Food Engineering, of established markets, giving some guidance as to the capacity necessary. (​If contraction is required, there is probably another business issue with which to deal.) whether they could operate seven days a week 24 hours a day instead. Thus, capacity is not a single number, but rather a range with a time span and  @angel for the 40.166666 issue, you can use TimeSpan.TotalHours as integet and then TimeSpan.Minutes and TimeSpan.Seconds to format the string the way you want it. – Bala R May 20 '11 at 15:55 @angel, see my edit, you can use FromHours() to convert 40 hours – Bala R May 20 '11 at 16:18

Python Datetime Tutorial: Manipulate Times, Dates, and Time Spans, Become a master of times and dates in Python as you work with the hour, and minute; Getting weekday and day of year; Converting date We can also antipate some problems. Let's give it a date string in YYYY-MM-DD format and see what it can do! python datetime tutorial - timedelta and time span  Hi folks I want to convert minutes into readable stuff for the endusers, e.g.: I have 2.480,08 minutes I want to format this in duration (but not in Time-Format) because the value is greater than 24 hours. Result shoud be: 1 day 08:22:16 But without VAR - this slowdown extremly all my visualiza

  • So what is the input of man_hours_nbr.Text, and what culture are you using? It seems that the input' format differences from the system datetime format.
  • Hours can only go from 0 to 23.
  • I need the total hours to be displayed not days since they represent the number of hours worked on a project
  • You can't directly parse 25 hours to a TimeSpan, as Hours only go up to 23. What you can do is parse it to days and then get the total hours from the TimeSpan
  • docs.microsoft.com/en-us/dotnet/api/…
  • Thanks but when I tried it in debug mode, it<s not making it to double hours = ts.TotalHours;