Warning: strstr() [function.strstr]: Empty delimiter in [path]/includes/class_postbit.php(294) : eval()'d code on line 167

Warning: strstr() [function.strstr]: Empty delimiter in [path]/includes/class_postbit.php(294) : eval()'d code on line 167

Warning: strstr() [function.strstr]: Empty delimiter in [path]/includes/class_postbit.php(294) : eval()'d code on line 167

Warning: strstr() [function.strstr]: Empty delimiter in [path]/includes/class_postbit.php(294) : eval()'d code on line 167
TimeSpan Problem - C#(C Sharp)
TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Notices

TimeSpan Problem

C#(C Sharp)


Reply
 
Thread Tools Display Modes
Old 09-11-2007, 01:35 AM   #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.


  Reply With Quote
Old 09-11-2007, 01:35 AM   #2
Nicholas Paldino [.NET/C# MVP]
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.
>
>



  Reply With Quote
Old 09-11-2007, 01:36 AM   #3
koredump
Guest
 
Posts: n/a
Re: TimeSpan Problem

What you need is ts.TotalMinutes



  Reply With Quote
Old 09-11-2007, 01:36 AM   #4
Chris Shepherd
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.
  Reply With Quote
Old 09-11-2007, 01:36 AM   #5
Ignacio Machin \( .NET/ C# MVP \)
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.
>
>



  Reply With Quote
Old 09-11-2007, 01:36 AM   #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.
>>
>>

>
>



  Reply With Quote
Old 13-02-2009, 09:35 PM   #7
Newbie
 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 NayZaw is an unknown quantity at this point


OS: Windows Vista


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
NayZaw is offline   Reply With Quote
Reply

Thread Tools
Display Modes



< Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 09:46 PM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional