Solved

C# Calculate the difference between two hours

Posted on 2013-05-16
2
618 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

Technology Partners: 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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