Hi ..
I found that CDate function has a strange behavior in the following code :
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" + Application.StartupPath + @"\db.mdb" + ";Persist Security Info=False");
{
OleDbDataAdapter adapterA = new OleDbDataAdapter("SELECT CDate(#01/05/2013#) AS TheDate", connection);
DataTable dataTableA = new DataTable();
adapterA.Fill(dataTableA);
DateTime dateTimeA = (DateTime)dataTableA.Rows[0]["TheDate"]; // get 1
MessageBox.Show(dateTimeA.Month.ToString());
} //these to ensure that I did not use the variables in the next block /^-^\ .
{
OleDbDataAdapter adapterB = new OleDbDataAdapter("SELECT CDate(#13/05/2013#) AS TheDate", connection);
DataTable dataTableB = new DataTable();
adapterB.Fill(dataTableB);
DateTime dateTimeB = (DateTime)dataTableB.Rows[0]["TheDate"]; // get 5
MessageBox.Show(dateTimeB.Month.ToString());
}
}
I understand that if the value is bigger than 12, the CDate function will consider it as the 'Day' part of the date and the other part will be considered as the 'Month' part of the date.
what is the rule for that ? why Microsoft did not explain this in the MSDN ?
What a Strange behavior of CDate ?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

VulpesRank #55 · 29.2K reputationPosted Jan 16, 2013, 6:34 AM EST
However, you should be able to avoid ambiguity problems with Date literals in VBA, regardless of locale, by always expressing them in #yyyy/mm/dd# format.