Solved

C# Calculate the difference between two hours

Posted on 2013-05-16
2
620 Views
Last Modified: 2013-05-16
I am using to dropdownlist boxes to allow users to select a start and end time.  I need to know the delta between the two.

so if ddlStart = 2:00 pm and ddlEnd = 3:00 pm then I need the delta 1 returned.
if however ddlStart = 2:00 pm and ddlEnd = 3:45 pm then I need the delta 1.75 returned.

Any ideas?
0
Comment
Question by:CipherIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39171916
string ddlStart = "2:00 PM";
string ddlEnd = "3:45 PM";
TimeSpan start = DateTime.Parse(ddlStart).TimeOfDay;
TimeSpan end = DateTime.Parse(ddlEnd).TimeOfDay;
TimeSpan diff = end - start;
double delta = diff.Hours + (diff.Minutes / 60D);

Open in new window

0
 
LVL 10

Expert Comment

by:eguilherme
ID: 39171924
Do you have those 2 values in a DateTime or TimeSpan object?

If so, its as simple as:

 //2:00 PM
var ddlStart = new DateTime(1900, 1, 1, 14, 0, 0);
//3:45 PM
var ddlEnd = new DateTime(1900, 1, 1, 15, 45, 0);

var Delta = ddlEnd.Subtract(ddlStart).TotalHours;
//Delta = 1.75

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question