Geo Geo - 1 year ago 77
C# Question

Why DateTime.TryParse returning false when given a real year string?

In the code below I am giving the function a sTransactionDate="1999" and I am trying to covert it to a date x/x/1999.

DateTime dTransactionDate = new DateTime();
if(DateTime.TryParse(sTransactionDate, out dTransactionDate))
{ //Happy
{ //Sad

if the string is "1999" it will always end up in sad. Any ideas?

Answer Source

Try something like this (adjust the CultureInfo and DateTimeStyles appropriately):

   out dTransactionDate)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download