Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:



I am developing a web application using asp.net c#. In that I need to get the percentage of Time.
I am not getting the correct way to accomplish this. I have TotalHours which is coming from db.
I need to calculate the 80% of that TotalHours.
Please help me. Thanks in adv.....

TimeSpan TotalTime = TimeSpan.Parse(totalhours);

if(TotalTime != null)
{

TimeSpan Percentage= ( TotalTime* 80 ) / 100;
// here I need to get correct percentage in hrs
// If TotalTime is 10 hrs then Percentage should be 8 hrs 
}
Posted
Updated 4-Jul-15 0:32am
v2

1 solution

you can use TotalHours property of TimeSpan
C#
TimeSpan TotalTime =(DateTime.Now.AddHours(10)-DateTime.Now); // sample code give you 10 hours for testing 
double Percentage =hours.TotalHours*.8; // for above value you will get 8 as percentage
 
Share this answer
 
Comments
George Swan 4-Jul-15 18:23pm    
You may need to be a bit careful here as the fractional part of an hour is often expressed as base 60, so 10.40 means 10hrs 40 minutes not 10hrs 24minutes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900