TimeSpan in C#

  • Thread starter Thread starter prasanta.bhowmik
  • Start date Start date
P

prasanta.bhowmik

Hello,

I created a object of TimeSpan only with minute and expected the
result in hours and minute, but its returning wrong result.
My Code is :

TimeSpan timeSpan = new TimeSpan(0,2400,0);
Console.WriteLine(timeSpan.Hours+':'+timeSpan.Minutes);

Its Displaying me the result is 16:00, but expected result is 40:00.

But if I am trying to create an object of 75 minutes the its resulting
the correct result of 1hours and 15 minuts.
So why the same code is not working with 2400 minute.

Please do the needful.

Thanks and Regards,
Prasanta
 
Hello,

I created a object of TimeSpan only with minute and expected the
result in hours and minute, but its returning wrong result.
My Code is :

TimeSpan timeSpan = new TimeSpan(0,2400,0);
Console.WriteLine(timeSpan.Hours+':'+timeSpan.Minutes);

Its Displaying me the result is 16:00, but expected result is 40:00.

But if I am trying to create an object of 75 minutes the its resulting
the correct result of 1hours and 15 minuts.
So why the same code is not working with 2400 minute.

Please do the needful.

Thanks and Regards,
Prasanta

I think the minutes variable is limited to 60 minutes, ie it doesnt roll
over automaticaly.
I think you can use AddMinutes wich takes an argument of as many minutes as
it can hold in total.

Colin =^.^=
 
Hello,

I created a object of TimeSpan only with minute and expected the
result in hours and minute, but its returning wrong result.
My Code is :

TimeSpan timeSpan = new TimeSpan(0,2400,0);
Console.WriteLine(timeSpan.Hours+':'+timeSpan.Minutes);

Its Displaying me the result is 16:00, but expected result is 40:00.
From the docs for TimeSpan.Hours:

<quote>
The hour component of this instance, between 0 and 23.
</quote>

If you use TotalHours, you'll get 40. The problem is that you've
created something with 1 day, 16 hours and 0 minutes, which is why
you're seeing the 16.

Jon
 
Chances are the extra hours are being converted into days. Try out
TotalHours to get the total number of hours (40 in your case).

Dave
 
I think the minutes variable is limited to 60 minutes, ie it doesnt roll
over automaticaly.

Nope - as the OP said, 75 minutes gave him 1 hour 15 minutes. The
problem is that 40 hours = 1 day + 16 hours, and the Hours property is
limited to 0-23. The created TimeSpan was fine, it was just the use of
it which was incorrect.

Jon
 
Jon Skeet said:
Nope - as the OP said, 75 minutes gave him 1 hour 15 minutes. The
problem is that 40 hours = 1 day + 16 hours, and the Hours property is
limited to 0-23. The created TimeSpan was fine, it was just the use of
it which was incorrect.

Jon

Ah yes i didnt take it in properly.
Id just had a similar problem of trying to set the minutes to more than 60
too.

Colin =^.^=
 
Back
Top