Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime?.ToString(format) [duplicate]

Possible Duplicate:
How can I format a nullable DateTime with ToString()?

got issue parsing DateTime? to specific format. Like:

 DateTime t1 = ...;
 string st1 = t1.ToString(format); //<-- works

 DateTime? t1 = ...;
 string st1 = t1.ToString(format); //Dont work.

Theres no overload method for DateTime?

like image 489
VoonArt Avatar asked May 15 '12 12:05

VoonArt


People also ask

What is T and Z in timestamp?

The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC).

How do I format a date string?

The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.

How do I convert DateTime to string?

Convert DateTime to String using the ToString() Method Use the DateTime. ToString() method to convert the date object to string with the local culture format. The value of the DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.


2 Answers

if (t1.HasValue)
    string st1 = t1.Value.ToString(format);
like image 163
abatishchev Avatar answered Oct 19 '22 13:10

abatishchev


Use Coalesce Operator

DateTime? t1 = ...;

string st1 = t1 ?? t1.Value.ToString(format);
like image 3
Nikhil Agrawal Avatar answered Oct 19 '22 12:10

Nikhil Agrawal