Difference between time

  • Thread starter Thread starter Paperback Writer
  • Start date Start date
P

Paperback Writer

How could I extract the difference bewteen times like this:

4:56:09am 4:59:10

The difference is: 00:03:01

How do I accomplish this ?

Thanks in advance,
Daniel
 
Paperback said:
How could I extract the difference bewteen times like this:

4:56:09am 4:59:10

The difference is: 00:03:01

How do I accomplish this ?

By subtracting two DateTime instances, you get a TimeSpan object, which
has the information you want. Like this:

DateTime timeFrom = new DateTime(2005, 8, 24, 4, 56, 9);
DateTime timeTo = new DateTime(2005, 8, 24, 4, 59, 10);
TimeSpan difference = timeTo - timeFrom;

Now you can use the members of the TimeSpan structure, like Hours,
Minutes, Seconds and others, to evaluate the difference.


Oliver Sturm
 
Paperback said:
How could I extract the difference bewteen times like this:

4:56:09am 4:59:10

The difference is: 00:03:01

How do I accomplish this ?

Try something like that:

DateTime t1 = DateTime.Parse("4:56:09");
DateTime t2 = DateTime.Parse("4:59:10");

TimeSpan dt = t2-t1;

System.Diagnostics.Trace.WriteLine(dt);
prints "00:03:01"

hth,
Max
 
Back
Top