Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullable DateTime to String [duplicate]

Tags:

c#

datetime

I want to convert Nullable DataTime to string.

DateTime datetime = DateTime.Now;
string strdatetime = datetime.ToString("MM/dd/yyyy");

Above coding working fine, non nullable DateTime.

DateTime? datetime = DateTime.Now;
string strdatetime = datetime.ToString("MM/dd/yyyy");

This one showing error No overload for method 'ToString' takes 1 arguments.

like image 525
Jesuraja Avatar asked Dec 05 '22 06:12

Jesuraja


1 Answers

try this one,

 string strdatetime = datetime.HasValue ? datetime.Value.ToString("MM/dd/yyyy") : string.Empty;
like image 83
KarthikManoharan Avatar answered Dec 25 '22 05:12

KarthikManoharan