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");

Advertisement

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.