How to Get the Time Interval Based on Minutes in C#
Submitted by janobe on Sunday, April 14, 2019 - 22:01.
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.
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 time interval based on minutes.- private void get_minutes(TimeSpan ts,ListBox lst)
- {
- int total_minutes, total_hours;
- try
- {
- total_hours = ts.Hours * 60;
- total_minutes = ts.Minutes + total_hours;
- lst.Items.Add(total_minutes);
- }catch(Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
Step 4
Write the following code to get the interval in minutes 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_minutes(ts, listBox1);
- }
Add new comment
- 139 views