Search code examples
c#functiondatetimemethodsdatetimepicker

I can't start method when using DateTime


I have some problem with my code... I can't see the messagebox when the datetime is initialized. What is wrong with the code? I can't find any errors at all. I'm using a WPF modified application that supports DateTimePicker. It looks like this:

    private void Start_Click(object sender, RoutedEventArgs e)
    {
        startjob();
    }
    private void startjob()
    {
        DateTime start = (DateTime)DateTimePicker1.Value;
        DateTime end = (DateTime)DateTimePicker2.Value;
        DateTime now = DateTime.Now;
        if ((now > start) && (now < end))
        {
            System.Windows.MessageBox.Show("It works");
        }

XAML:
<wpfTool:DateTimePicker x:Name="DateTimePicker1" Margin="506,189,192,1137" Width="227" TimeFormat="LongTime">
                </wpfTool:DateTimePicker>
                <wpfTool:DateTimePicker x:Name="DateTimePicker2" Margin="506,238,192,1088" Width="227" Height="25">
                    </wpfTool:DateTimePicker>

Solution

  • DateTime? start = DateTimePicker1.Value;
    DateTime? end = DateTimePicker2.Value;
    DateTime now = DateTime.Now;
    
    if (start == null || end == null)
    {
        // one of the pickers is empty
    }
    else if (now >= start.Value && now <= end.Value)
    {
        // you selected values in range of "now"
    }