How to Determine the Date Based on the Given Days in C#

In this tutorial, I will teach you how to determine the date based on the given days using c#. This method has the ability to know the exact date after you input the days' interval on a textbox. This is like a calculator that easily calculates the dates for you. Check the procedure below to see how it works.

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

Open the code editor by pressing the F7 on your keyboard. In the code editor, create a method that will calculate the date ahead.
  1.  
  2.         private void determine_date(TextBox days,DateTimePicker start_date)
  3.         {
  4.             int date_Interval;      //REPRESENTS A DATE INTERVAL
  5.             DateTime  date_result;  //REPRESENTS THE ADDED DATE OF START DATE AND THE INTERVAL DAYS
  6.             string str_msg;       //REPESENTS A MESSAGE TO PUT INTO A LISTBOX
  7.  
  8.             try
  9.             {
  10.                 if(days.Text.Length > 0)
  11.                 {
  12.                     date_Interval = int.Parse(days.Text);
  13.  
  14.                     date_result = start_date.Value.AddDays(date_Interval);
  15.  
  16.                     str_msg = "After " + date_Interval + " day(s), the date is ";
  17.  
  18.                     listBox1.Items.Clear();
  19.                     listBox1.Items.Add(str_msg + date_result.ToString("MM/dd/yyyy hh:mm") );
  20.  
  21.                
  22.                 }
  23.             }
  24.             catch (Exception ex)
  25.             {
  26.                 MessageBox.Show(ex.Message);
  27.             }
  28.         }

Step 4

Add the following codes to see the result when the button is clicked.
  1.  
  2.         private void button1_Click(object sender, EventArgs e)
  3.         {
  4.             determine_date(textBox1, dateTimePicker1);
  5.         }
The complete source code is included you can download it and run it on your computer. 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