0

Is it possible to do this?I am trying to have three separate drop down boxes with one for the hours(hh), one for the minutes(mm) and the last for seconds(ss) and parsinfg them and making it just one time.

Here is my code that I used for a text box;

 TimeSpan ts1 = TimeSpan.Parse(txtStart.Text); 
 TimeSpan ts2 = TimeSpan.Parse(txtEnd.Text);
 txtDisplay.Text = (ts2 - ts1).ToString();   

I used this code for two textboxes and figured that it would be harder for the user to enter time so I tried putting a dropdownbox instead to ease everything for the user and it keeps giving me an error.

Here is my code;

    TimeSpan ts2 = TimeSpan.Parse(txtEnd.Text);
    TimeSpan hh = TimeSpan.Parse(drpdwn1.SelectedValue);
    TimeSpan mm = TimeSpan.Parse(drpdwn2.SelectedValue);
    TimeSpan ss = TimeSpan.Parse(drpdwn3.SelectedValue);

    txtDisplay.Text = (ts2 - (hh:mm:ss)).ToString();

I'm only using ts2 for an example here, later on if this works I will add three more drop down boxes to ease the entering of a start and end time for a user. Can someone help me find the right code?

1 Answers1

0

You need to create an actual DateTime object and work with that.

DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 
   Convert.ToInt32(drpdwn1.SelectedValue), 
   Convert.ToInt32(drpdwn2.SelectedValue), 
   Convert.ToInt32(drpdwn3.SelectedValue));
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Is there a way where I could go about this method and not include the date and just the time? – Josaia Malugulevu Nov 01 '17 at 11:20
  • Why, they are one and the same. – VDWWD Nov 01 '17 at 11:20
  • What if the second time goes onto another day and I try to subtract the two? Lets say for example the start time of my graveyard shift starts from 9pm and ends at 5am the next day. – Josaia Malugulevu Nov 01 '17 at 11:53
  • See https://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days – VDWWD Nov 01 '17 at 12:09
  • So far i have this code: 'DateTime starttime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Convert.ToInt32(drpdwn1.SelectedValue), Convert.ToInt32(drpdwn2.SelectedValue), Convert.ToInt32(drpdwn3.SelectedValue));' – Josaia Malugulevu Nov 01 '17 at 12:28
  • And this: DateTime endtime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Convert.ToInt32(drpdwn4.SelectedValue), Convert.ToInt32(drpdwn5.SelectedValue), Convert.ToInt32(drpdwn6.SelectedValue)); DateTime maxtime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.MaxValue.Hour, DateTime.MinValue.Minute, DateTime.MinValue.Second); – Josaia Malugulevu Nov 01 '17 at 12:29
  • if (starttime > endtime) txtDisplay.Text = (endtime + (maxtime - starttime)).ToString(); else txtDisplay.Text = (endtime - starttime).ToString(); – Josaia Malugulevu Nov 01 '17 at 12:29
  • If I was to try and calculate the amount of hours I did in my graveyard shift I keep getting the current date and 07:00:00 displayed in my textbox – Josaia Malugulevu Nov 01 '17 at 12:31
  • Use `TimeSpan`. You can see examples in the link i posted in the comments. – VDWWD Nov 01 '17 at 12:41