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

Status
Not open for further replies.

lilpatotie

Eternal Poster
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.webp

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

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
 
Status
Not open for further replies.

About this Thread

  • 3
    Replies
  • 531
    Views
  • 3
    Participants
Last reply from:
lilpatotie

Trending Topics

Online now

Members online
1,075
Guests online
837
Total visitors
1,912

Forum statistics

Threads
2,274,957
Posts
28,959,592
Members
1,233,493
Latest member
Alluneediswalk
Back
Top