Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to have the user input a date in one datepicker, then underneath input the number of weeks between the two dates. The second datepicker will then assign that date weeks, days or years later.

What I have tried:

I have the two datepickers and the textbox:
<datepicker x:name="Date1"
="" focusable="False" displaydate="01/01/2022" displaydatestart="01/01/2022" displaydateend="12/31/2022" istodayhighlighted="True">


<textbox x:name="Text 1"
="" tooltip="Enter the number of weeks between dates" background="AliceBlue" borderbrush="Black" borderthickness="2" textalignment="Center" horizontalalignment="Stretch">

<datepicker x:name="Date2"
="" focusable="False" displaydate="01/01/2022" displaydatestart="01/01/2022" displaydateend="12/31/2022">
Posted
Updated 19-Oct-22 6:11am
Comments
Richard Deeming 19-Oct-22 7:30am    
Take the number of weeks, multiply by 7, and pass that to the DateTime's AddDays method. What's the problem?
Member 15802897 19-Oct-22 9:41am    
How would you do that? I tried this way (if (int.TryParse(Text1.Text, out int weeksToAdd) && Date1.SelectedDate.HasValue)
Date2.SelectedDate = Date1.SelectedDate.Value.AddDays(7 * weeksToAdd);) but I'm still getting an error.
Richard Deeming 19-Oct-22 9:52am    
What error?
Ralf Meier 19-Oct-22 10:48am    
Show the complete code from this ... and tell when it is called ...
Member 15802897 19-Oct-22 10:40am    
It says that the nullable type might be null
And that datepicker does not contain a Add days feature

1 solution

Richard did not suggest using Add on the Datepicker.
As this is an assignment I will not give you the code, but here are the steps to follow and some reference material you may find useful

1. Create a DateTime variable
2. Assign the Date value from the first DatePicker to that variable
3. Extract the number of weeks from the TextBox - you might find Int32.TryParse Method (System)/a>[^] useful
4. Multiply the number of weeks by 7 to get the number of days to add
5. Use the datetime.adddays[^] to add the results from step 4 to the variable you populated in step 2
6. Assign the value of the results of step 5 to the second datepicker
 
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