Solved

C# Calculate the difference between two hours

Posted on 2013-05-16
2
612 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
2 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now