We help IT Professionals succeed at work.

Cannot implicitly convert type 'string' to 'System.DateTime?

Medium Priority
6,044 Views
Last Modified: 2013-12-16
Is there a simple way to convert a data field that has be defined as a string into an actual datetime?
myCRpatient.DECEASED_DATE = patient.p.deceasedDate;---------squiggly under patient.p.deceasedDate

Open in new window

Comment
Watch Question

Commented:
DateTime.Parse & DateTime.TryParse lets you convert a String into a DateTime object.
More information about those methods here...
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx


Good Luck!
kwh3856Owner

Author

Commented:
oxyoo
I tried

myCRpatient.DECEASED_DATE = DateTime.TryParse(patient.p.deceasedDate);
It then told me no overload for method.
Tony McCreathTechnical SEO Consultant

Commented:
The syntax is as follows
if (DateTime.TryParse(patient.p.deceasedDate, out myCRpatient.DECEASED_DATE))
{
  // success, valid date format
}
else
{
  // failed, invalid date format
}

Open in new window

Commented:
Either use the syntax above (suggested by Tiggerito) which is more "safe" or use:


myCRpatient.DECEASED_DATE = DateTime.Parse(patient.p.deceasedDate);

Open in new window

Commented:
kwh3856,

I see that you have a few of these posts for multiple data type conversions.  Most (all?) data type classes and structures in .NET have a TryParse() method that accepts a string to convert and a variable on which to store the converted value.  The method returns true if parsing succeeded, or false if it failed.  For example:
    if (Int32.TryParse(myString, out myInt))
    {
        // Success! myInt now contains the numeric value of myString
    }
    else
    {
        // Failed! MyInt is not defined.
    }

The TryParse() method only converts from strings to other types.  For conversions between different types, you can use the Convert class.  It has methods to convert to multiple types, such as:
   Convert.ToString()
   Convert.ToInt32()
   Convert.ToByte()
   Convert.ToDate()
   Convert.ToDouble()
   // etc.

Each method is overloaded to accept most data types.  Here's more information on these methods:
    http://articles.techrepublic.com.com/5100-10878_11-5834604.html

And lastly, if you want to convert anything to a string, this is even easier! Every object has a ToString() method that "stringifies" its value.  For example, to convert an Integer to a string, use:
     myIntegerValue.ToString()
And to convert a DateTime value to a string, use:
     myDateTimeValue.ToString()

     I hope this helps.
     -dZ.
kwh3856Owner

Author

Commented:
tiggerito,
When I tried your code I got this error message
A property or indexer may not be passed as an out or ref parameter
 

if (DateTime.TryParse(patient.p.deceasedDate, out myCRpatient.DECEASED_DATE))------ squiggle under myCRpatient.Deceased_date
 
{
  // success, valid date format
}
else
{
  // failed, invalid date format
}

Open in new window

kwh3856Owner

Author

Commented:
tiggerito,
I tried this and got an invalid arguments error message.

if (DateTime.TryParse(patient.p.deceasedDate.ToString, out myCRpatient.DECEASED_DATE))
                    {
                        // success, valid date format
                    }
                    else
                    {
                        // failed, invalid date format
                    }

Open in new window

Commented:
If DECEASED_DATE is a property you can not use it with the out keyword. Properties are a kind of syntactic sugar methods and methods are not allowed to be used in conjunction with out.
Try this instead:

DateTime tmpDateTime = myCRpatient.DECEASED_DATE;
if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime))
                    {
                        // success, valid date format
                    }
                    else
                    {
                        // failed, invalid date format
                    }

Open in new window

kwh3856Owner

Author

Commented:
oxyoo,
When I tried your example I got
Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)
That occured here

DateTime tmpDateTime = myCRpatient.DECEASED_DATE;  ------ squiggle under myCRpatient.DECEASED_DATE
 
and
The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
This occured here

if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime))---squiggle line from DateTime all the the way to the last parenthesis
 

Commented:
Hi,
This means that myCRpatient.DECEASED_DATE is nullable. I have adapted the code below (I made the DateTime nullable), and it should work now:
Read about nullables here:
http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx


DateTime? tmpDateTime = myCRpatient.DECEASED_DATE;
if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime))
                    {
                        // success, valid date format
                    }
                    else
                    {
                        // failed, invalid date format
                    }

Open in new window

kwh3856Owner

Author

Commented:
oxyoo,
That fixed the first error message but I am still getting the same second error message.  Any ideas?
The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
Thanks
Kenny
 
Tony McCreathTechnical SEO Consultant

Commented:
ToString should be ToString()
Commented:
I'm sorry I was a bit quick. TryParse can not be used with nullables. Here is an updated example:
DateTime? tmpDateTime = myCRpatient.DECEASED_DATE;
if (tmpDateTime.HasValue) {
  DateTime tmpDateTime2 = tmpDateTime.Value;
  
  if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime2)) {
    // success, valid date format
  } else {
    // failed, invalid date format
  }
} else {
  // failed, DateTime is null
}

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
kwh3856Owner

Author

Commented:
oxyoo,
Thanks for the update but I still get the error on
The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
error occurs on this line

if (DateTime.TryParse(patient.p.deceasedDate.ToString, out tmpDateTime2))
I am still trying to digest the information your giving me so I am not sure what to try to fix it.
kwh3856Owner

Author

Commented:
Got it... i read your above post.....had to add parens to the tostring command

DateTime? tmpDateTime = myCRpatient.DECEASED_DATE;
if (tmpDateTime.HasValue)
{
DateTime tmpDateTime2 = tmpDateTime.Value;
if (DateTime.TryParse(patient.p.deceasedDate.ToString(), out tmpDateTime2))
{
// success, valid date format
}
else
{
// failed, invalid date format
}
}
else
{
// failed, DateTime is null
}
kwh3856Owner

Author

Commented:
Thank you very much for sticking out with me.  I am still trying to digest the code but I just need to chew on for awhile and I will understand what you did.  Thank you very much.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.