How to Determine the Date Based on the Given Days in C#
Submitted by janobe on Saturday, April 27, 2019 - 17:14.
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.
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.
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
Open the code editor by pressing the F7 on your keyboard. In the code editor, create a method that will calculate the date ahead.- private void determine_date(TextBox days,DateTimePicker start_date)
- {
- int date_Interval; //REPRESENTS A DATE INTERVAL
- DateTime date_result; //REPRESENTS THE ADDED DATE OF START DATE AND THE INTERVAL DAYS
- string str_msg; //REPESENTS A MESSAGE TO PUT INTO A LISTBOX
- try
- {
- if(days.Text.Length > 0)
- {
- date_Interval = int.Parse(days.Text);
- date_result = start_date.Value.AddDays(date_Interval);
- str_msg = "After " + date_Interval + " day(s), the date is ";
- listBox1.Items.Clear();
- listBox1.Items.Add(str_msg + date_result.ToString("MM/dd/yyyy hh:mm") );
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
Step 4
Add the following codes to see the result when the button is clicked.- private void button1_Click(object sender, EventArgs e)
- {
- determine_date(textBox1, dateTimePicker1);
- }
Add new comment
- 103 views