1

I have a DateTimePicker called dtProcurationDate it show the date as custom format yyyy/M/d .. now i'm trying to get only the year from it with code

string year = dtProcurationDate.Value.Year.ToString();

also tried

this.dtProcurationDate.CustomFormat = "yyyy";
string year = dtProcurationDate.Value.Year.ToString();

but it gives me a null value ..

5
  • 1
    That simply can't happen. The Value property of a datetime picker is of type DateTime, that is a struct so it can't be null. The year prpperty is an int, another struct. Plus if it was possible to have a null value you would get a null reference exception. Something else is going on. Did you try debugging your code? Commented Sep 30, 2017 at 19:15
  • yes .. it gives me a null value in the debugging mode Commented Sep 30, 2017 at 19:21
  • Which value is null? Commented Sep 30, 2017 at 19:34
  • year value ... i'm sure that i did something stupid .. still trying Commented Sep 30, 2017 at 19:43
  • Looking at the reference source it has a contract declaration that says the result will not be null, so something else must be going on. It would help if you posted a complete example because the fragment doesn't reveal anything. Commented Sep 30, 2017 at 23:31

1 Answer 1

2
//syntax is datetimepicker.Value.Year

int year = dtProcurationDate.Value.Year; //your case
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.