Solved

# time Diff Problem

Posted on 2007-10-01
357 Views
time diff value problem
the problem acores when the Start time is 21:00
and the EndTime is 06:00
i need to get this value : 9 houres
insted i am geting this value : -15
i try to add more 1 day to the EndTime but its dosent work
this is the code :

public string CalcHour(DateTime dtS,DateTime dtE)
{

dtE = new DateTime(dtE.Year,dtE.Month,dtE.Day,dtE.Hour,dtE.Minute,0);
dtS = new DateTime(dtS.Year,dtS.Month,dtS.Day,dtS.Hour,dtS.Minute,0);

TimeSpan ts = dtE.Subtract(dtS);
string difference1 = ts.Hours.ToString() + "." + ts.Minutes.ToString().PadLeft(2,'0');
decimal diff = Decimal.Parse(difference1.ToString());

if(diff < 0)

return difference1;

}
string difference1;
public string CalcHourNegtive(DateTime dtS,DateTime dtE)
{

dtE = new DateTime(dtE.Year,dtE.Month,dtE.Day,dtE.Hour,dtE.Minute,0);
dtS = new DateTime(dtS.Year,dtS.Month,dtS.Day,dtS.Hour,dtS.Minute,0);

TimeSpan ts = dtE.Subtract(dtS);
difference1 = ts.Hours.ToString() + "." + ts.Minutes.ToString().PadLeft(2,'0');
decimal diff = Decimal.Parse(difference1.ToString());
return difference1;

}
how can i coorect it ?

thanks ....
0
Question by:Tech_Men

LVL 22

Expert Comment

Set a breakpoint at the start of CalcHour. When you hit it, Quickwatch dtE and dtS. Are you getting any date values in, or only hours, minutes and seconds?

Jim
0

Author Comment

only dates
0

LVL 19

Expert Comment

Why not just add 24 to ts.TotalHours?
0

LVL 22

Expert Comment

Now I am confused - If you get "only dates", where are you getting any time information?

Jim
0

LVL 19

Expert Comment

Or change "CalcHourNegtive(dtS,dtE.Date.AddDays(1));" to "CalcHourNegtive(dtS,dtE.AddDays(1));".  You are removing the time information from your computed DateTime so the result is incorrect when you do the subtraction.

Or, since you're only concerned about times within one day (or at least I assume so based on the simplistic differencing), then just do:

TimeSpan ts;
if (dtS < dtE) { ts = dtE.Subtract(dtS); }
else { ts = dtS.Subtract(dtE); }
return ts.Hours.ToString() + "." + ts.Minutes.ToString().PadLeft(2,'0');
0

LVL 22

Expert Comment

I rewrote your method as this:
public string CalcHour(DateTime dtS, DateTime dtE)
{
string difference1 = string.Empty;
dtE = new DateTime(dtE.Year, dtE.Month, dtE.Day, dtE.Hour, dtE.Minute, 0);
dtS = new DateTime(dtS.Year, dtS.Month, dtS.Day, dtS.Hour, dtS.Minute, 0);

TimeSpan ts = dtE.Subtract(dtS);
if (ts.Hours < 0)
{
difference1 = (ts.Hours + 24).ToString() + "." + ts.Minutes.ToString().PadLeft(2, '0');
}
else
{
difference1 = ts.Hours.ToString() + "." + ts.Minutes.ToString().PadLeft(2, '0');
}
return difference1;

}

Then called it with this:
DateTime now = DateTime.Now;
string span = CalcHour(now, later);

When I stepped through it, it did not need to add 24 hours, and the result was: "12:00".

So, I have to conclude there is something wrong upstream of this method. Something is sending you bad dates.

Jim
0

LVL 19

Accepted Solution

And my math skills have disappeared today.  You cannot do the last suggestion I made.  You CAN do this, however:

TimeSpan ts = dtE.Subtract(dtS);
if (ts.Hours < 0) { ts = ts.Add(new TimeSpan(24, 0, 0)); }
return ts.Hours.ToString() + "." + ts.Minutes.ToString().PadLeft(2, '0');
0

Author Comment

thanks alloiot to all....
0

## Featured Post

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…