Monday, July 2, 2007

Nullable DateTime



A variable of type DateTime in C# cannot be assigned to a null value. However, in some cases, we do need to do that.


In such cases, we can use the Nullable version of DateTime. Two suggested ways of assigning a null value to DateTime are:


1. Nullable<DateTime> currentDate = null

2. DateTime? currentDate = null


One Problem I faced while using the Nullable DateTime is that, when I tried to convert the date into string I was not able to specify the any format as there is no overload for DateTime?.ToString() which takes one or more arguments like DateTime.ToString().


Nullable are actually Generic, Value properly gives directly the DateTime inside the Generics.


This is what works.




DateTime? delvDate;

delvDate = DateTime.Now;

string str = delvDate.Value.ToString(“yyyy-MM-dd”);


Following is the link which talks about the workarounds and Nullable in .Net2.0

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258


DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

No comments:

Post a Comment