Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I have simple WPF application, and I need to let user choose value from Calendar and then work with the day only. For example, user chooses date 06/08/2020, so I need to take the "06" value and assign it to int variable, then work with it in some mathematical equations.

Unfortunately I did not find on the interner, how to exactly do it. When the user enters number into textbox, its quiet easy, you just state the variable and put the TextBox.Text as a value, like int x = int.Parse(TextBox1.Text);


Can you guys help me on this?


What I have tried:

I was trying some variations like this

int day = Calendar1.SelectedDate.Day, but non of it worked


Thank you very much
Posted
Updated 16-Aug-20 1:49am

WPF Date Picker control:
Quote:
A DatePicker is a control that allows a user to pick a date value. The user picks the date by using ComboBox selection for month, day, and year values

Reference: WPF - Datepicker - Tutorialspoint[^]

Sample:
C#
private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e) { 
     var picker = sender as DatePicker;  
     DateTime? date = picker.SelectedDate;
		
     if (date == null) { 
        this.Title = "No date"; 
     } 
     else { 
        var daySelected = date.Day; // Day from the selected date
     } 
  }

References:
DateTime.Day Property (System) | Microsoft Docs[^]
In case you are looking for day of week: How to: Extract the Day of the Week from a Specific Date | Microsoft Docs[^]
 
Share this answer
 
The Calendar has a SelectedDate property - which returns a DateTime value.
So you can get the day of the month from that as normal:
C#
DateTime dt = myCalendar.SelectedDate;
int dayOfMonth = dt.Day;
If that doesn't work, then either the user hasn't selected a date (use DateTime.HasValue to check that) or he has selected multiple dates, in which case you probably want to look at the Calendar.SelectedDates collection instead and decide which one to use.
 
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