How to Compute Sum of Date and Number of Days in C#
Submitted by donbermoy on Friday, June 20, 2014 - 11:40.
Last week, I have discussed a program for computing the difference between two dates. See here: How to Compute Date Difference in C#. Now, i will teach you how to create a program for finding a sum of a date and the number of days to be added using C#.
So, now let's start this tutorial!
1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as DateSum.
2. Next, add one DateTimePicker named DateTimePicker1 in the ToolBox. Add also one TextBox named TextBox1 for the inputting of the number of days to add. Then add a button named Button1 labeled as "Compute Date Sum". You must design your interface like this:
3. In your Form_Load, make the DateTimePicker value into the current date.
4. In your Button1, put this code below.
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
- private void Form1_Load(object sender, EventArgs e)
- {
- DateTimePicker1.Value = DateTime.Today;
- }
- public void Button1_Click(System.Object sender, System.EventArgs e)
- {
- // initialize variable i as integer
- int i = default(int);
- // initialize variable dt as DateTime
- DateTime dt = default(DateTime);
- // the dt variable will hold the current date value of DateTimePicker1
- dt = DateTimePicker1.Value;
- // convert the string value of textbox1 to integer that will hold into variable i
- i = Convert.ToInt16(TextBox1.Text);
- // display the sum using the DateAdd method with DateInterval, inputted number of days in i variable, and dt variable for current date
- MessageBox.Show("The date sum is:" + " " + DateAndTime.DateAdd(DateInterval.Day, i, dt));
- }
Add new comment
- 200 views