Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Calculate the difference between two hours

Posted on 2013-05-16
2
Medium Priority
?
631 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 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

927 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