Getting the Date
part of a DateTime
object didn't workout for me because I'm working on the client-side and the returned web service values have some null
dates. As a result, it tries to get the Date part of a null value and it throws a runtime exception. The following example is how I solved my problem:
string dt = employer.BirthDay.ToString();if(dt == ""){ dt = "N/A";}else dt = dt.Substring(0,10);
- Get the DateTime value as string into a string variable.
- Check if it's null. If null, assign a string variable.
- If not null, get the first 10 characters of the string
DateTime
value and assign it to the string variable.
I'm sharing this for future reference.