Handle negative time spans
Asked Answered
Z

9

48

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.

Zugzwang answered 19/6, 2009 at 15:38 Comment(2)
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).Oneiric
Note that a difference between 2 TimeOnly values is different -it is always positive and can be confusing #73040186Hip
P
108

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.

Percussion answered 27/11, 2011 at 22:37 Comment(1)
This is the easiest way, just subtract your two DateTimes, call Duration() on the result, and then do TotalHours.Henotheism
U
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.

Utu answered 19/6, 2009 at 16:14 Comment(0)
T
10

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

Tramway answered 19/6, 2009 at 15:41 Comment(1)
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".Wellknown
M
9

There is a Negate method in the TimeSpan class.

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

Marozik answered 1/11, 2011 at 11:47 Comment(0)
S
3

The simple solution would be to do:

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

hours = Math.Abs(hours)
Sigler answered 19/6, 2009 at 15:42 Comment(0)
M
2

its working .try this

mytimespam.Negate();

Mccreery answered 2/1, 2015 at 11:26 Comment(1)
time spam, hehe ;)Flurried
T
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");

Troup answered 18/11, 2014 at 15:19 Comment(0)
S
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);
}
Spam answered 20/2, 2018 at 9:4 Comment(1)
Much more simple way is to just compare if (diff < TimeSpan.Zero)Pforzheim
K
1
TimeSpan Diff = Date1 - Date2;

if ((int)Diff.TotalDays < 0) { // your code }
Karylkarylin answered 22/6, 2019 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.