Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In windows application I need to convert a HH:MM timespan into decimal HH.MM.. Please help me with the same urgently

What I have tried:

TimeSpan StartTime = TimeSpan.Parse(dtpStartTime.Value.ToString("HH:MM"));
TimeSpan EndTime = TimeSpan.Parse(dtpEndTime.Value.ToString("HH:MM"));

if (StartTime <= EndTime)
{
WorkingHours = (EndTime - StartTime);
double th;

int j= Convert.ToInt32(TotalDays.ToString());
for (int i = 1; i <= j; i++)
{

TotalHour =+ WorkingHours;
th=Convert.ToDecimal((TotalHour).ToString(#.00));

}
Posted
Updated 2-May-18 1:15am
Comments
Patrice T 2-May-18 7:31am    
Show an autonomous piece of code (that we can run).
And sample input and output (with minutes)

1 solution

It depends on how you want the final value to be repesented. See the TimeSpan Structure (System)[^]. Use the TotalXxx() properties to get the total value as double with fractions or the other properties to get the days / hours / minutes / seconds parts as integers.

Don't work with strings. Use the numeric values for calculation and convert finally to string for output.
 
Share this answer
 
Comments
CPallini 2-May-18 7:19am    
5.

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