how to get time difference

J

Jassim Rahma

I have the following dates:

17:03:2008 21:15:58
19:47:2008 13:07:07


and i want to get the time difference between the two dates in the
following formats:

1. int format, for example: 9,253 minutes
2. hh:mm format, for example: 18:17 or even 218:17


Many Thanks,
Jassim Rahma
 
M

Marc Gravell

?? 19:47:2008 ??

Something like below perhaps:

DateTime dt1 = DateTime.Parse("17/03/2008 21:15:58"),
dt2 = DateTime.Parse("19/07/2008 13:07:07");

TimeSpan delta = dt2 - dt1;

string foo = ((int)Math.Floor(delta.TotalMinutes)).ToString(),
bar = string.Format("{0}:{1}",
(int)Math.Floor(delta.TotalHours), delta.Minutes);

Marc
 
A

Arne Vajhøj

Marc said:
Something like below perhaps:

DateTime dt1 = DateTime.Parse("17/03/2008 21:15:58"),
dt2 = DateTime.Parse("19/07/2008 13:07:07");

TimeSpan delta = dt2 - dt1;

string foo = ((int)Math.Floor(delta.TotalMinutes)).ToString(),
bar = string.Format("{0}:{1}",
(int)Math.Floor(delta.TotalHours), delta.Minutes);

I would drop the Math.Floor and use {1,00} ...

Arne
 
M

Marc Gravell

I would drop the Math.Floor

Fair enough - I'm just too lazy to remember all the different rounding
rules; by making it explicit it means I don't have to think to often ;-
p

(especially when re-visiting an old bit of code)

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top