Time Interval in C#

Today in C#, i will teach you to get a time interval in C#. In here, we will use the TimeSpan properties for getting the time interval. It segregates the hours, minutes, seconds, millisecond and even the tick of the clock. So, now let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application and name it as TimeInterval. 2. Next, add two DateTimePicker named dtpstrTime for the starting time and dtpEndTime for the end time. Insert 5 textboxes named txthours for displaying hours, txtminutes for displaying minutes, txtseconds for displaying seconds, txtmillesecond for displaying millisecond, and txtticks for displaying ticks. Add also a Button named btnGo. You must design your interface like this: output 3. Create a sub procedure named TimeInterval for separating the time intervals of the corresponding fields, put this code below:
  1. private void TimeInterval(TimeSpan ts)
  2.                 {
  3.                        
  4.                         //use the properties of the timespan and it demonstrate  timespan.hours
  5.                         //,  timespan.milliseconds ,  timespan.minutes  , timespan.seconds
  6.                         // and timespan.ticks
  7.                         try
  8.                         {
  9.                                
  10.                                 txthours.Text = ts.Hours.ToString();
  11.                                 txtmillisecond.Text = ts.Milliseconds.ToString();
  12.                                 txtminutes.Text = ts.Minutes.ToString();
  13.                                 txtseconds.Text = ts.Seconds.ToString();
  14.                                 txtticks.Text = ts.Ticks.ToString();
  15.                         }
  16.                         catch (Exception ex)
  17.                         {
  18.                                 MessageBox.Show(ex.Message, this.Text);
  19.                         }
  20.                 }
4. For btnGo, put this code below to get the time interval.
  1.                 public void btngo_Click(System.Object sender, System.EventArgs e)
  2.                 {
  3.                         try
  4.                         {
  5.                                
  6.                                 //create a variable of a timespan
  7.                                 TimeSpan time_span = new TimeSpan();
  8.                                 //create the variable of a datetime
  9.                                 DateTime strt_Date = default(DateTime);
  10.                                 DateTime end_Date = default(DateTime);
  11.                                
  12.                                
  13.                                 //set and convert the time from the datetimepicker
  14.                                 strt_Date = DateTime.Parse(dtpstrTime.Text);
  15.                                 end_Date = DateTime.Parse(dtpEndtime.Text);
  16.                                 //subtract the starting time and the end time
  17.                                 time_span = end_Date.Subtract(strt_Date).Duration();
  18.                                
  19.                                
  20.                                 //perform the sub procedure that you have
  21.                                 //created in displaying the time interval
  22.                                 TimeInterval(time_span);
  23.                         }
  24.                         catch (Exception Ex)
  25.                         {
  26.                                 MessageBox.Show(Ex.Message, this.Text);
  27.                         }
  28.                 }
5. Now, put this code in your Form_Load.
  1.                 public void Form1_Load(System.Object sender, System.EventArgs e)
  2.                 {
  3.                         //set the value of the end date higher than the start date
  4.                         dtpEndtime.Value = DateAndTime.DateAdd(DateInterval.Hour, 9, dtpEndtime.Value);
  5.                         dtpEndtime.Value = DateAndTime.DateAdd(DateInterval.Minute, 30, dtpEndtime.Value);
  6.                         dtpEndtime.Value = DateAndTime.DateAdd(DateInterval.Second, 30, dtpEndtime.Value);
  7.                        
  8.                 }
Output: output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Comments

hello, if I set for start time 23.59.00, and for end time 00.00.00, some days or one day next for end time, I have 23 hour and 1 minute left

Add new comment