time Diff Problem

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)
                        CalcHourNegtive(dtS,dtE.Date.AddDays(1));

                  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 ....
Tech_MenAsked:
Who is Participating?
 
drichardsCommented:
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
 
JimBrandleyCommented:
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
 
Tech_MenAuthor Commented:
only dates
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
drichardsCommented:
Why not just add 24 to ts.TotalHours?
0
 
JimBrandleyCommented:
Now I am confused - If you get "only dates", where are you getting any time information?

Jim
0
 
drichardsCommented:
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
 
JimBrandleyCommented:
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;
DateTime later = now.AddHours(12);
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
 
Tech_MenAuthor Commented:
thanks alloiot to all....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.