Date formatting on DateTime?

  • Thread starter Thread starter Shikari Shambu
  • Start date Start date
S

Shikari Shambu

I have DateOfBirth declared as

DateTime? DateOfBirth;

DateOfBirth.ToSTring("yyyyMMdd");

gives an error - no overload method ToString() takes 1 arguments.

However, if I were to declare DateOfBirth as

DateTime DateOfBirth;

this works without an error.

How do I get the formatting to work on the nullable DateTime I.e DateTime?
DateOfBirth;

TIA
 
Shikari Shambu said:
I have DateOfBirth declared as

DateTime? DateOfBirth;

DateOfBirth.ToSTring("yyyyMMdd");

Use DateOfBirth.Value.ToString("yyyyMMdd");
 
Figured this out. Have to explicitly give .Value.

DateOfBirth.Value.ToString("yyyyMMdd");
 
Shikari Shambu skrev:
I have DateOfBirth declared as

DateTime? DateOfBirth;

DateOfBirth.ToSTring("yyyyMMdd");

gives an error - no overload method ToString() takes 1 arguments.

However, if I were to declare DateOfBirth as

DateTime DateOfBirth;

this works without an error.

How do I get the formatting to work on the nullable DateTime I.e
DateTime? DateOfBirth;
Time to learn about how to use nullable variables?

string text = DateOfBirth.HasValue ?
DateOfBirth.Value.ToString("yyyyMMdd") : "-";
 
I have DateOfBirth declared as

DateTime? DateOfBirth;

DateOfBirth.ToSTring("yyyyMMdd");

gives an error - no overload method ToString() takes 1 arguments.

However, if I were to declare DateOfBirth as

DateTime DateOfBirth;

this works without an error.

How do I get the formatting to work on the nullable DateTime  I.e DateTime?
DateOfBirth;

Check to make sure it's not null, then cast it to DateTime.
 
Back
Top