What's new

Closed How to Get the Hours Interval of Two Given Time in C#

Status
Not open for further replies.

lilpatotie

Eternal Poster
Established
Joined
Sep 29, 2017
Posts
1,133
Reaction
330
Points
335
Age
35
590839


In this tutorial, I will teach you how to get the hours interval of two given time in c#. This method has the ability to get the difference between two given time. It generates if how many hour intervals in the two given time that will be displayed inside the listbox. Let's begin.

Creating Application

Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
creating_application_c_3.png

Step 2
Do the form just like shown below.
ps_form1.png

Step 3
Press F7 to open the code editor and create a method for getting the hour interval between two given time.
  1. private void get_hours(TimeSpan ts,ListBox lst)
  2. {
  3. try
  4. {
  5. lst.Items.Clear();
  6. lst.Items.Add(ts.Hours.ToString());
  7. }catch(Exception ex)
  8. {
  9. MessageBox.Show(ex.Message);
  10. }
  11. }

Step 4



Write the following codes to get the hours interval when the button is clicked.
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. TimeSpan ts;
  4. DateTime date_from,date_to;
  5. date_from = DateTime.Parse (dtp_From .Text );
  6. date_to = DateTime.Parse(dtp_To .Text);
  7. ts = date_to.Subtract(date_from);
  8. get_hours(ts, listBox1);
  9. }

NOTE: I don't own this tutorial or application. I just share this for more knowledge.
Credits to the Owner
 

Attachments

Status
Not open for further replies.

Similar threads

Back
Top