How to return TimeSpan without formatting?

var timeSpan = new TimeSpan(10,130,10);

after the execution of above line, normally the value of timeSpan is formatted as 12:10:10

Is there any chance can I get value as 10:130:10 (i.e., without formatting)? I need it for a critical situation.

Answers


I don't believe there is a way you get 10:130:10 after you define your TimeSpan constructor. And there is no reason to keep them becuase 10 hours + 130 minutes + 10 seconds is equal to 12:10:10 as we all know. It is a time interval, not keeps time components separately.

From TimeSpan(Int32, Int32, Int32) constructor;

The specified hours, minutes, and seconds are converted to ticks, and that value initializes this instance.

Let's look at how this contructor defined;

public TimeSpan(int hours, int minutes, int seconds)
{
    _ticks = TimeToTicks(hours, minutes, seconds);
}

And this is how TimeToTicks methods implemented;

internal static long TimeToTicks(int hour, int minute, int second)
{
    long totalSeconds = (long)hour * 3600 + (long)minute * 60 + (long)second;
    if (totalSeconds > MaxSeconds || totalSeconds < MinSeconds)
       throw new ArgumentOutOfRangeException(null, Environment.GetResourceString("Overflow_TimeSpanTooLong"));
    return totalSeconds * TicksPerSecond;
}

As you can see, this method doesn't keep constructor parameters (hours, minute or second). It just calculate totalseconds from hours * 3600 + minute * 60 + second value.


TimeSpan doesn't work like that. It represents an elapsed duration of time, not individual time components. See Soner's excellent answer for further details.

If you are looking to keep "10 hours, 130 minutes, 10 seconds" as separate information, then you should consider the ISO-8601 duration format. As a string, that value would look like "PT10H130M10S".

In .NET, you can use the Period type from the Noda Time library to work with these sort of values.

To create a single part period, you can take advantage of simple factory methods like Period.FromMonths(3). But to create a multi-part period, you will need to do one of the following approaches:

  • You can use a PeriodBuilder to build a period from individual variables:

    PeriodBuilder builder = new PeriodBuilder();
    builder.Hours = 10;
    builder.Minutes = 130;
    builder.Seconds = 10;
    
    Period period = builder.Build();
    string s = period.ToString();
    
    Debug.WriteLine(s); // "PT10H130M10S"
    
  • You can parse a period from an ISO-8601 duration string, using a PeriodPattern. In particular, the RoundtripPattern shown below will retain the parts exactly as they were originally supplied:

    string s = "PT10H130M10S";
    PeriodPattern pattern = PeriodPattern.RoundtripPattern;
    ParseResult<Period> result = pattern.Parse(s);
    if (result.Success)
    {
        Period p = result.Value;
    
        Debug.WriteLine(p.Hours);   // 10
        Debug.WriteLine(p.Minutes); // 130
        Debug.WriteLine(p.Seconds); // 10
    }
    

Need Your Help

How to clear filter value in toolbar if row is removed from advanced search dialog

jquery twitter-bootstrap jquery-ui jqgrid free-jqgrid

Free jqgrid does not clear values in filter toolbar if they are removed from advanced searxh dialog.

Javascript/jQuery: Split camelcase string and add hyphen rather than space

javascript regex split camelcasing

I would imagine this is a multiple part situation with regex, but how would you split a camelcase string at the capital letters turning them in to lowercase letters, and then adding a hyphen betwee...