![]() |
|
|
#1 |
|
Guest
Posts: n/a
|
TimeSpan Problem
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. |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Re: TimeSpan Problem
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] - <msnews.microsoft.com> wrote in message news:.gbl... > 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. > > |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Re: TimeSpan Problem
What you need is ts.TotalMinutes
|
|
|
|
#4 |
|
Guest
Posts: n/a
|
Re: TimeSpan Problem
msnews.microsoft.com wrote:
> 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. |
|
|
|
#5 |
|
Guest
Posts: n/a
|
Re: TimeSpan Problem
Hi,
Take a look at TotalMinutes -- Ignacio Machin Mobile & warehouse Solutions. <msnews.microsoft.com> wrote in message news:.gbl... > 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. > > |
|
|
|
#6 |
|
Guest
Posts: n/a
|
Re: TimeSpan Problem
Perfect. Didn't even notice TotalMinutes. Thanks a lot.
"Nicholas Paldino [.NET/C# MVP]" <> wrote in message news:.gbl... > 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] > - > > <msnews.microsoft.com> wrote in message > news:.gbl... >> 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. >> >> > > |
|
|
|
#7 |
|
Newbie
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
![]() OS:
|
Re: TimeSpan Problem
Hi All,
![]() I have a problem with TimeSpan when I want to compute and compare the time duration of two tasks or more. So I tried with simple code to make sure the problem is with TimeSpan. Here my codes are..... static void Main(string[] args) { long total = 0; DateTime startTime1 = DateTime.Now; for (int count = 0; count < 100; count++) { total += count; } DateTime stopTime1 = DateTime.Now; /* Compute and print the duration of the first task. */ TimeSpan duration1 = stopTime1 - startTime1; Console.WriteLine("First task duration: {0} milliseconds.", duration1.TotalMilliseconds); /* Now I want to measure another task which has more duration time. */ DateTime startTime2 = DateTime.Now; for (int count = 0; count < 10000; count++) { total += count; } DateTime stopTime2 = DateTime.Now; /* Compute and print the duration of this second task. */ TimeSpan duration2 = stopTime2 - startTime2; Console.WriteLine("Second task duration: {0} milliseconds.", duration2.TotalMilliseconds); } When I run it, the result was shown First task duration : 4 milliseconds. Second task duration : 0 milliseconds. Press any key to continue............... I don't understand why it happened. When I used the code Thread.Sleep(1200) in the place of for loop count at second tasks, TimeSpan worked correctly. Please give me some solution for that problem. Thank you all in advance. Nay Zaw ![]() |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |