How to Get the Time Interval Based on Minutes in C#

Today I’m going to teach you how to get the time interval based on minutes in C#. This procedure is based on my last tutorial which is “How to Get the Hours Interval of Two Given Time in C#”. This program will illustrates how to get the difference of two given time in minutes. This is simple yet powerful method when you are dealing with the time.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#. ps1

Step 2

Do the form just like shown below. ps2

Step 3

Press F7 to open the code editor and create a method for getting the time interval based on minutes.
  1.    
  2.         private void get_minutes(TimeSpan ts,ListBox lst)
  3.         {
  4.             int total_minutes, total_hours;
  5.  
  6.             try
  7.             {
  8.                 total_hours = ts.Hours * 60;
  9.                 total_minutes = ts.Minutes + total_hours;
  10.                  
  11.                 lst.Items.Add(total_minutes);
  12.  
  13.             }catch(Exception ex)
  14.             {
  15.                 MessageBox.Show(ex.Message);
  16.             }
  17.         }
  18.        

Step 4

Write the following code to get the interval in minutes when the button is clicked.
  1.    
  2.         private void button1_Click(object sender, EventArgs e)
  3.         {
  4.             TimeSpan ts;
  5.             DateTime date_from,date_to;
  6.  
  7.             date_from = DateTime.Parse (dtp_From .Text );
  8.             date_to = DateTime.Parse(dtp_To .Text);
  9.             ts = date_to.Subtract(date_from);
  10.  
  11.             get_minutes(ts, listBox1);
  12.  
  13.         }
For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below

Add new comment