Nullable Date Format in C#

If we have to format the Nullable Date in C#, We can do like this

DateTime? date = new DateTime(2021, 05, 10, 22, 45, 12, 004);
//Will through exception
// string date_str = date.ToString("dd/MM/yyyy");

//Working Syntax:
string date_str= (date != null ? date.Value.ToString("dd/MM/yyyy") : "n/a");


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.