3

How do you truncate the seconds bit from a timespan object in C#? i.e. 15:37

I'm outputting a timespan object to JavaScript in the format of HH:mm and kind of want the server side to process providing the correct format instead of clients browsers, can that be done without providing this as a C# string object to JavaScript?

Maya
  • 1,414
  • 5
  • 22
  • 43
  • If you're formatting the timespan as HH:mm aren't the seconds already truncated? – Chris Van Opstal Feb 24 '11 at 22:46
  • Do you really want to set the seconds to 0 in a TimeSpan, or just omit them when converting to string? – CodesInChaos Feb 24 '11 at 22:46
  • Have a look at this answer http://stackoverflow.com/questions/338658/can-you-round-a-net-timespan-object/338705#338705 – Will Dean Feb 24 '11 at 22:52
  • @CodeInChaos @fredrik-mork @Nick Yes omit the seconds without converting to string, my JSON serializer will need to get hh:mm in TimeSpan format without converting to String – Maya Feb 24 '11 at 22:53
  • 1
    @Maya: a `TimeSpan` is a value, not a string representation of a value. If you need it in a specific format, you will need to convert it to a string. – Fredrik Mörk Feb 24 '11 at 23:04
  • So perhaps the real question is: "How do I add a raw string when serializing to json without the serializer automatically quoting the string." – CodesInChaos Feb 24 '11 at 23:15

5 Answers5

20

You can use a format string for that:

public string GetTimeSpanAsString(TimeSpan input)
{
    return input.ToString(@"hh\:mm");
}
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
9

You can truncate the 'ticks' value which is the core of a TimeSpan:

TimeSpan t1 = TimeSpan.FromHours(1.551);
Console.WriteLine(t1);
TimeSpan t2 = new TimeSpan(t1.Ticks - (t1.Ticks % 600000000));
Console.WriteLine(t2);

Gives:

01:33:03.6000000
01:33:00
Will Dean
  • 39,055
  • 11
  • 90
  • 118
  • Thanks but I need 01:33 output in my case – Maya Feb 24 '11 at 22:57
  • @Maya "1:33" is a string. You just said you wanted a TimeSpan and not a string. – CodesInChaos Feb 24 '11 at 22:59
  • Then you need to send a preformatted string, not a TimeSpan. I read the question has 'how do I do this without sending a string', sorry. – Will Dean Feb 24 '11 at 23:00
  • @CodeInChaos @will-dean thanks Will I thought there is a way to produce the hh:mm format AS a TimeSpan without converting to String. – Maya Feb 24 '11 at 23:02
  • 1
    @Maya I think you're going to have to either modify your JSON serializer or do this on the client side. Or just modify the value you're sending to your serializer and send a string instead. – Chris Van Opstal Feb 24 '11 at 23:02
  • 1
    A TimeSpan has no format. It's just an integral number of ticks. I think what you want is changing how it's serialized to json, and not the TimeSpan itself. – CodesInChaos Feb 24 '11 at 23:04
  • 1
    @Maya - TimeSpan doesn't have any particular string representation until someone converts it into a string - pretty much all .NET types work like this. Though you could create one which carried its format with it, that isn't the way the standard types work. – Will Dean Feb 24 '11 at 23:06
  • Ok fair enough, that clears things up for me, many thanks guys, a +1 vote for you all :) – Maya Feb 24 '11 at 23:07
  • 2
    Great answer, but I would use TimeSpan.TicksPerMinute instead of 600000000. – Darren May 29 '15 at 09:36
1

Maybe not optimal, but easy to read:

TimeSpan.FromMinutes((long)duration.TotalMinutes);
xmedeko
  • 7,336
  • 6
  • 55
  • 85
0

I believe this is what you're looking for.

string.Format("{0:H:mm}",myTime)
The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
  • That code crashes if `myTime` is a `TimeSpan` (*System.FormatException: Input string was not in a correct format.*) – Fredrik Mörk Feb 24 '11 at 22:52
  • @Fredrik, sorry, however `H:mm` should be the converter your looking for. – The Muffin Man Feb 24 '11 at 22:59
  • I don't understand what you mean. `H:mm` is not a valid [format string for a `TimeSpan`](http://msdn.microsoft.com/en-us/library/ee372287.aspx). For a `DateTime` yes, but not for `TimeSpan`. – Fredrik Mörk Feb 24 '11 at 23:03
  • @Fredrik, I was under the impression that it would parse the string for a time and convert it to the specified format. I apologize for giving you false information. – The Muffin Man Feb 24 '11 at 23:06
0

Perhaps something like this. This truncates to minutes using the truncation of an integer division, followed by a multiplication by the divisor.

return TimeSpan.FromTicks(input.Ticks/TicksPerMinute*TicksPerMinute);
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262