- Joined
- Sep 29, 2017
- Posts
- 1,133
- Reaction
- 330
- Points
- 335
- Age
- 35
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#.
Step 2
Do the form just like shown below.
Step 3
Press F7 to open the code editor and create a method for getting the hour interval between two given time.
- private void get_hours(TimeSpan ts,ListBox lst)
- {
- try
- {
- lst.Items.Clear();
- lst.Items.Add(ts.Hours.ToString());
- }catch(Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
Step 4
Write the following codes to get the hours interval when the button is clicked.
- private void button1_Click(object sender, EventArgs e)
- {
- TimeSpan ts;
- DateTime date_from,date_to;
- date_from = DateTime.Parse (dtp_From .Text );
- date_to = DateTime.Parse(dtp_To .Text);
- ts = date_to.Subtract(date_from);
- get_hours(ts, listBox1);
- }
NOTE: I don't own this tutorial or application. I just share this for more knowledge.
Credits to the Owner
Attachments
-
You do not have permission to view the full content of this post. Log in or register now.