Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I m creating a program in c# winform which will tell the exact date from the entered days. Here I have taken 2 DateTimePicker and 1 Textbox.

For Example :

DateTimePicker1.Text = textbox1.Text + DateTimePicker2.Text; // It will show the exact date from the days entered.

I want to produce this output in the Textbox :

01-11-2022 + 180 days = 30-04-2023

Plz let me know. If anyone you have the code or an algorithm let me know, as I m new to c#.

What I have tried:

I have not tried any code yet.
Posted
Updated 2-Nov-22 19:01pm
Comments
[no name] 3-Nov-22 0:44am    
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.adddays?view=net-7.0

First off, stop using the Text property of things, and use more "natural" vlaues.

The DateTimePicker has a Value propertyValue Property[^] which returns the selected date as a DateTime object. Use that and you can do maths with it. If you have two textboxes txtDaysForward and txtResult, then you need to convert the former to an integer value and then add that to the DateTimePicker.Value.

You start by converting a user entered string to an integer.
C#
if (!int.TryParse(txtDaysForward.Text, out int daysForward))
   {
   ... report a problem to the user - that's not a number ...
   return;
   }

Then you can set your result:
C#
DateTime start = MyDateTimePicker.Value;
txtResult.Text = $"{start} + {daysForward} = {start.AddDays(daysForward)}";

Or set your second DateTimePicker to the resulting date:
C#
DateTime start = MyDateTimePicker.Value;
ResultDateTimePicker.Value = start.AddDays(daysForward);


Give it a try - you'll see what I mean.
 
Share this answer
 
v2
Comments
Nikhil Yogi 3-Nov-22 3:32am    
Thanks So much! It worked. Thanks once again...
OriginalGriff 3-Nov-22 4:56am    
You're welcome!
var dt1 = dateTimePicker2.Value;
var dt2 = dt1.AddDays(int.Parse(textBox1.Text));
dateTimePicker1.Value = dt2;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900