47

In my output of a grid, I calculate a TimeSpan and take its TotalHours. e.g.

(Eval("WorkedHours") - Eval("BadgedHours")).TotalHours

The goal is to show the TotalHours as 39:44, so I need to convert the value from 7.5 to 07:30. This is no problem... unless it's negative!

I can create a TimeSpan object from Hours with

TimeSpan.FromHours( (Eval("WorkedHours") - Eval("BadgedHours")).TotalHours)

If it's negative, I can't convert it to a DateTime to use the .ToString("HH:mm") method, and the TimeSpan object does not support the format string.

Michael
  • 8,362
  • 6
  • 61
  • 88
  • Please consider reformatting the code snippets as inline code or code sections, and add the appropriate platform tag (I'd guess .NET from your snippets). – OregonGhost Jun 19 '09 at 15:42

9 Answers9

106

Isn't there a TimeSpan.Duration method? I think this would handle what you are trying to do.

Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.

Jon
  • 9,156
  • 9
  • 56
  • 73
Jared
  • 7,165
  • 6
  • 49
  • 52
  • 7
    This is the easiest way, just subtract your two DateTimes, call Duration() on the result, and then do TotalHours. – Dan May 23 '14 at 15:15
12
static string ToHMString(TimeSpan timespan) { 
    if (timespan.Ticks < 0) return "-" + ToHMString(timespan.Negate());

    return timespan.TotalHours.ToString("#0") + ":" + timespan.Minutes.ToString("00");
}

Console.WriteLine(ToHMString(TimeSpan.FromHours(3)));       //Prints "3:00"
Console.WriteLine(ToHMString(TimeSpan.FromHours(-27.75)));  //Prints "-28:45"

This will also work correctly if the timespan is longer than 24 hours.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
10

Just multiply it by -1 or use an absolute value function.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 54
    For anyone who googled it: use `TimeSpan.Duration` method. From MSDN: "Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object". – prostynick Jan 21 '11 at 11:17
9

There is a Negate method in the TimeSpan class.

Link to the MSDN documentation: TimeSpan.Negate Method()

MCollard
  • 926
  • 2
  • 20
  • 39
Eugeniu Torica
  • 7,484
  • 12
  • 47
  • 62
3

The simple solution would be to do:

string format = "HH:mm";
if(hours < 0)
  format = "-" + format;

hours = Math.Abs(hours)
Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
Jerod Venema
  • 44,124
  • 5
  • 66
  • 109
2

its working .try this

mytimespam.Negate();

Deval Patel
  • 107
  • 6
1

Hi i worked this into a bit of code i have been writing, hope it helps

(results) is an int variable

(TimeSpan.FromMinutes(result)) < TimeSpan.Zero ? "-" + TimeSpan.FromMinutes(result).ToString(@"hh\:mm") : "" + TimeSpan.FromMinutes(result).ToString(@"hh\:mm");

1

I checked to every where.But i didnt get a correct answer that why i used this way to finish

TimeSpan diff = actualout.Subtract(actualin);
 string a =(diff.ToString()).ToString();
if(a.Contains("-"))
 {        
 diff = new TimeSpan(0,0,0,0);
}
Cristik
  • 30,989
  • 25
  • 91
  • 127
sajin k
  • 11
  • 1
1
TimeSpan Diff = Date1 - Date2;

if ((int)Diff.TotalDays < 0) { // your code }
brcebn
  • 1,571
  • 1
  • 23
  • 46