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

Submitted by: 
Language: 
Visitors have accessed this post 87 times.

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.