Wednesday, 5 December 2012

Difference between two DateTimes C#


Difference between two DateTimes C#

  • One date is  less than two date  all values will be negative
  • one date is equal and only time diff, total hours,mins,sec will have values
  • One date is greater than two date all values will be positive
 public int GetTimeDifference(DateTime one, DateTime two)
      {
          if (DateTime.Compare(one, two) <= 0)
          {
              TimeSpan tspan = one.Subtract(two);
              Console.WriteLine(tspan.TotalDays);
              Console.WriteLine(tspan.TotalHours);
              Console.WriteLine(tspan.TotalMinutes);
              Console.WriteLine(tspan.TotalSeconds);
              Console.WriteLine(tspan.TotalMilliseconds);
          }
          return 1;
      }