Solved

C# Calculate the difference between two hours

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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