TimeSpan Problem

G

Guest

Why is it that:

DateTime start = DateTime.Parse("1:00 AM");
DateTime stop = DateTime.Parse("2:30 AM");
TimeSpan ts = stop - start;
Response.Write(ts.Minutes.ToString());

Produces 30, not 90? How do I return the exact number of minutes between 2
times?

Thanks a lot.
 
C

Chris Shepherd

msnews.microsoft.com said:
Why is it that:

DateTime start = DateTime.Parse("1:00 AM");
DateTime stop = DateTime.Parse("2:30 AM");
TimeSpan ts = stop - start;
Response.Write(ts.Minutes.ToString());

Produces 30, not 90? How do I return the exact number of minutes between 2
times?

Because the Minutes property of the TimeSpan struct represents the
minutes in the hour, not the total number of minutes. Just use the
TotalMinutes property.

Chris.
 
N

Nicholas Paldino [.NET/C# MVP]

Because the number of minutes on the resulting TimeSpan is 30. If you
want to get the total number of minutes, you need to access the TotalMinutes
property.
 
G

Guest

Perfect. Didn't even notice TotalMinutes. Thanks a lot.

Nicholas Paldino said:
Because the number of minutes on the resulting TimeSpan is 30. If you
want to get the total number of minutes, you need to access the
TotalMinutes property.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Why is it that:

DateTime start = DateTime.Parse("1:00 AM");
DateTime stop = DateTime.Parse("2:30 AM");
TimeSpan ts = stop - start;
Response.Write(ts.Minutes.ToString());

Produces 30, not 90? How do I return the exact number of minutes between
2 times?

Thanks a lot.
 

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