How to Get the Hours Interval of Two Given Time in C#
Submitted by janobe on Wednesday, April 10, 2019 - 14:45.
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.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
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);
- }
Add new comment
- 110 views