0

So I have 6 dropdown lists that store time values.

Start time
DDL Hour - 01, 02, 03,..., 10, 11, 12
DDL Minutes - 00, 15, 30, 45
DDL TimeofDay - AM, PM

Finish Time
Same values

I was looking for a way to, on index change, calculate the time taken between start and finish.

Is there a way to do this with the number of dropdown lists I have? I have seen several examples of people using 1 dropdown for start and 1 for finish. I could do that if all else fails but was hoping there was a way to concatenate the 3 together and then display the time taken.

maltman
  • 454
  • 1
  • 7
  • 28
  • 2
    I think what you would want to do is create a DateTime representing the start time, which is trivial if you look up the MSDN documentation on DateTime. Then you'd do the same for the finish time, and subtract one from the other. You would want to write a *method* which does all of this, and you would want to call that method from the index-changed event of all the dropdowns. I'd use the same event handler method for all of them. This will involve writing code, but some people do that all day long and yet live somewhat rewarding lives otherwise. – 15ee8f99-57ff-4f92-890c-b56153 Oct 06 '17 at 18:52
  • 1
    To add to what Ed says: subtracting one DateTime from another will give you a `TimeSpan` value. If you have a list of these `TimeSpan`s, then you can _sum_ them (which is what I think you mean by "concatenate") by calling `.Aggregate((t1, t2) => t1 + t2)`. – StriplingWarrior Oct 06 '17 at 18:58
  • Thanks Ed. I don't have an issue writing the code behind. Just wondering on combining the values. I will look at creating a date time because I have a calendar too that needs to go into this. – maltman Oct 06 '17 at 18:58
  • 2
    If there's not a specific date associated with your times, you might actually want to start with `TimeSpan`s instead of `DateTime`s in the first place. `new TimeSpan(hours, minutes, 0)`, where hours is set to Hour or Hour + 12, depending on AM/PM. – StriplingWarrior Oct 06 '17 at 19:02
  • @StriplingWarrior - [TimeSpan is not the same thing as time of day](https://stackoverflow.com/a/18648819/), so there are a few pitfalls with that approach. – NightOwl888 Oct 06 '17 at 19:06
  • If you subtract a TimeSpan from a DateTime, the expression returns a DateTime. – 15ee8f99-57ff-4f92-890c-b56153 Oct 06 '17 at 19:09
  • 1
    @NightOwl888: That's true. Technically if someone had a start time of 1h30 and an end time of 2h30, then my approach would always indicate that the meeting is an hour long. Akerra's approach would indicate that it's an hour long unless the current date is an anomalous day like those where the current time zone is entering or exiting DST. If there's a specific date associated with these start and end times then you could be more specific, but if there aren't then I don't think there are any *more correct* ways to calculate a duration than to use TimeSpans. – StriplingWarrior Oct 06 '17 at 20:34
  • @StripingWarrior please post something as an answer. I have used what you said and it works great. – maltman Oct 06 '17 at 20:58

2 Answers2

2

I think the simplest way is to get a DateTime value by combining the 3 dropdowns, you could do something like

DateTime time = DateTime.Today.AddHours(ddHours.SelectedValue).AddMinutes(ddMins.SelectedValue);
if (ddTimeOfDay.SelectedValue == "PM") time.AddHours(12);

Then repeat this for the other set of 3 dropdowns. To find the difference:

TimeSpan diff = timeFinish - timeStart;
akerra
  • 1,017
  • 8
  • 18
1

If you have text values, you might just combine everything to parse all at once:

var DDLStart_Hour = // start hours dropdown value
var DDLStart_Minute = // start minute dropdown value
var DDLStart_Tt = // start AM/PM dropdown value

var DDLEnd_Hour = // end hours dropdown value
var DDLEnd_Minute = // end minute dropdown value
var DDLEnd_Tt = // end AM/PM dropdown value

var now = DateTime.Now;
var start = DateTime.Parse($"{now.Month}/{now.Day}/{now.Year} {DDLStart_Hour}:{DDLStart_Minute}:00 {DDLStart_Tt}");
var end = DateTime.Parse($"{now.Month}/{now.Day}/{now.Year} {DDLEnd_Hour}:{DDLEnd_Minute}:00 {DDLEnd_Tt}");

var elapsed = end - start; // TimeSpan
BurnsBA
  • 4,347
  • 27
  • 39