Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8267587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:35:16+00:00 2026-06-08T05:35:16+00:00

Im trying to calculate the how many days there are between two dates and

  • 0

Im trying to calculate the how many days there are between two dates and display the result in a textblock, i am using wpf. However i get a nullable object must have a value in the first line :S

    private void button20_Click(object sender, RoutedEventArgs e)
    {
        DateTime start = datePicker1.DisplayDateStart.Value.Date;
        DateTime finish = datePicker2.DisplayDateStart.Value.Date;
        TimeSpan difference = start.Subtract(finish);
        textBlock10.Text = Convert.ToString(difference);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:35:19+00:00Added an answer on June 8, 2026 at 5:35 am

    As the error message implies, DisplayDateStart is a nullable property, which means it can (and, by default, does) have no value. You have to handle this condition to produce sensible results.

    That said, the DisplayDateStart property refers to the earliest date shown in the DatePicker’s calendar, not the date the user has picked: for that, you need the SelectedDate property, which is also nullable.

    There are a variety of ways you could handle a NULL value: display nothing in the TextBlock, display “N/A” or some other default, etc. Here’s an example:

    private void button20_Click(object sender, RoutedEventArgs e)
    {
        // This block sets the TextBlock to a sensible default if dates haven't been picked
        if(!datePicker1.SelectedDate.HasValue || !datePicker2.SelectedDate.HasValue)
        {
            textBlock10.Text = "Select dates";
            return;
        }
    
        // Because the nullable SelectedDate properties must have a value to reach this point, 
        // we can safely reference them - otherwise, these statements throw, as you've discovered.
        DateTime start = datePicker1.SelectedDate.Value.Date;
        DateTime finish = datePicker2.SelectedDate.Value.Date;
        TimeSpan difference = finish.Subtract(start);
        textBlock10.Text = difference.TotalDays.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying many ways to calculate the round number of days between two
So, I am trying to calculate the time between two dates that fits certain
I'm trying to calculate elapsed hours between two times. When calculating PM to AM
I'm trying to calculate how many minutes have elapsed between a saved DateTime and
am trying to calculate mean and variance using 3X3 window over image(hXw) in opencv...here
I am trying to calculate the sub_total field based on: total_full + total_half. however
I'm trying to calculate the number of pairwise differences between a long list of
i'm trying to use the GeoKit plugin to calculate the distance between 2 points.
I am trying to calculate the cube root of a many-hundred digit number modulo
I'm trying to write a simple game/utility to calculate poker odds. I know there's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.