The error that you've already mentioned in the comments,
Quote:
String was not recognized as a valid DateTime.
means that the String that you're passing to the
Convert.ToDateTime(argument);
is not a valid dateTime object. There are actually many things you need to consider while writing a string that can be converted to a DateTime object.
The syntax of the string, the month and date sequence, usage of
- or
/ might also raise this sort of error. It doesn't mean that your dateTime is not a human understandable dateTime but it means that the .NET framework doesn't understand the format of your DateTime.
For example if you use,
DateTime dateTime = new DateTime(12, 11, 2014);
To minimize this error, you need to be passing the parameters as
DateTime dateTime = new DateTime(2014, 11, 12);
Same stuff for the strings. While converting from String to a DateTime, you need to take a good care of the DateTime seperator (/ is used in .NET), the Month year day sequence etc.
This document[
^] of MSDN explains this concept a little bit accurately.
A sample code for you to pass a string would be,
DateTime dateTime = Convert.ToDateTime("05/01/1996");
Read
this[
^] article of mine to understand the DateTime in (
a little bit) depth.