Hi,
I have a property in a class with datatype Datetime?
for ex:
int Datetime? test;
Now i want to convert this test to "mm/dd/yyyy" format
I tried with .Tostring("mm/dd/yyyy") its not working.
it says an error
No overload for method ''Tostring" takes 1 argument.
Do anyone faced this problem before???
Thanks,
Prasant
Convert Datetime? property value to string format
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.


VulpesRank #55 · 29.2K reputationPosted Aug 16, 2012, 6:22 AM EDT
string s = (test != null) ? ((DateTime)test).ToString("MM/dd/yyyy") : "";
or you can use the HasValue and Value properties:
string s = test.HasValue ? test.Value.ToString("MM/dd/yyyy") : "";
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Aug 16, 2012, 6:57 AM EDT
The actual datetime value is present inside test.Value..
So instead of trying test.Tostring("mm/dd/yyyy")
try test.Value.Tostring("mm/dd/yyyy")
Sukesh MarlaRank #461 · 5.4K reputationPosted Aug 16, 2012, 8:56 AM EDT
Check this is correct answer if it really helped.