0

I have a problem regarding the DatePicker from the ToolBox. I'm trying to create a program in WPF for a user to choose two dates from two DatePickers, and based on these dates determine the amount of days between them.

Online I found another typ of tool called DateTimePicker, which seems easier to use. However, this is not included in my ToolBox so I'm forced to use the DatePicker-class.

So far my cs-code looks like below.

using System;
using System.Collections.Generic;

namespace DateDifference
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();


        }


        public static object RadDatePickerStart { get; private set; }

        private void button_Click(object sender, RoutedEventArgs e)
        {


        }


        private void startDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            // ... Get DatePicker reference.
            var picker = sender as DatePicker;

            // ... Get nullable DateTime from SelectedDate.
            DateTime? date = picker.SelectedDate;

            if (date == null)
            {
                // ... A null object.
                this.Title = "No date";
            }
            else
            {
                // ... No need to display the time.
                this.Title = "Program for calculating difference between dates";
                textBoxResult.Text = date.Value.ToShortDateString();

            }
        }

        private void endDate_SelectedDateChanged_1(object sender, SelectionChangedEventArgs e)
        {
            // ... Get DatePicker reference.
            var picker = sender as DatePicker;

            // ... Get nullable DateTime from SelectedDate.
            DateTime? date = picker.SelectedDate;

            if (date == null)
            {
                // ... A null object.
                this.Title = "No date";
            }
            else
            {
                // ... No need to display the time.
                this.Title = "Program for calculating difference between dates";
                textBoxResult.Text = date.Value.ToShortDateString();

            }
        }
    }
}

So my question is how to get the difference in days with this DatePicker-class?

Best regards!

jazy
  • 75
  • 4
  • 14
  • Possible duplicate of [Calculate difference between two dates (number of days)?](http://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days) – ASh Oct 28 '16 at 13:32
  • It's not clear what is missing: do you want to create another TextBox which displays the difference? If so you can simply bind the text to a dependency property (or your ViewModel) and update this property each time the end or start are changing... – Amittai Shapira Oct 28 '16 at 13:38

1 Answers1

0

You can get the datetime object from the datepicker and then compare the dates like this:

(EndDate - StartDate).TotalDays
Diemauerdk
  • 5,238
  • 9
  • 40
  • 56
  • How would I get the datetime object from the datepicker? Sorry for my basic questions. I´ve never used C# before. – jazy Oct 28 '16 at 13:52
  • try to set the value of the datetime object to null, datePicker.SelectedDate = null; You can also try something like this: datePicker.ClearValue(DatePicker.SelectedDateProperty) – Diemauerdk Oct 31 '16 at 07:30