• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

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 ....
0
Tech_Men
Asked:
Tech_Men
  • 3
  • 3
  • 2
1 Solution
 
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
 
drichardsCommented:
Why not just add 24 to ts.TotalHours?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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
 
Tech_MenAuthor Commented:
thanks alloiot to all....
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now