Link to home
Start Free TrialLog in
Avatar of Rahn
Rahn

asked on

How to check if a string can be converted to DateTime?

I need to check before execution if a string can be tranformed into a datetime.
ASKER CERTIFIED SOLUTION
Avatar of silemone
silemone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Reza Rad
DateTime.TryParse will try to parse a date value if possible...

works like this:
string date = "afdlsadf";
DateTime dateholder;
DateTime.TryParse(date, dateholder);
ooops correction

DateTime.TryParse will try to parse a date value if possible...

works like this:
string date = "afdlsadf";
DateTime dateholder;
DateTime.TryParse(date, out dateholder);
it needs the keyword 'out' because it's params are:

DateTime.TryParse(string x, out DateTime y)

out means that whatever variable you pass, if the string x you're passing is able to be converted, it will make that DateTime y that date.  

its equivalent of a return statement..
works like this:
string date = "afdlsadf";
DateTime dateholder;
DateTime.TryParse(date, dateholder);

obviously this will make dateholder = null;

string date = "May 4, 1976";
DateTime dateholder;
DateTime.TryParse(date, dateholder);

this will make dateholder = 5/4/1976 12:00:00
because string date is convertable

Hi Rahn;

This code shows how it can be done.

            // string testDate = "This is not a date";
            // string testDate = "1/2/2010";
            string testDate = "2010/01/02";

            DateTime dt;

            if (DateTime.TryParse(testDate, out dt))
            {
                MessageBox.Show(dt.ToString());
            }
            else
            {
                MessageBox.Show("String was not in the correct format.");
            }


Fernando
with that, i think I've given you a clear solution and fair explanation...if you have further questions, please ask...
you didn't say you needed to display it and i'm thinking you just want to ensure that nothing crashes...if there are problems, instead of messageboxes, you can actually just set a breakpoint and debug the code for more accurate states of each variable.
@ silemone;

With your last post you state that, "obviously this will make dateholder = null;", Objects of DateTime are NOT nullable and so DateTime.TryParse will set the variable dateholder to a value like 1/1/0001 12:00:00 AM. So putting it in an if statement to see if it is true or false is a good thing to test fore.

The statement, "DateTime.TryParse(date, dateholder);", needs the out parameter as you had in your other post.

Fernando
The message box is there to show how the code works and not necessarily to be coded that way.
@Fernando - that may be true at some point, but its still up to the author to decide if he wants that and if so, to ask for it  - i'm just stating his specs didn't ask for it,

I'm not saying your solution isn't one of many good solutions, it just doesn't follow his specs...the question is - How to check if a string can be converted to DateTime? - the most direct answer to that is use DateTime.Parse - the other stuff I gave was background information as I do with all answers I post - and finally, I still think debugging would be a better option...

Thanks for the correction on '= null'...i indeed mispoke.
@Fernando, gotcha on message box...now i understand better...
@ silemone;

I am not sure if in your post ID: 26120730 you are stating that Rahn question, "How to check if a string can be converted to DateTime?", that he is stating the data type DateTime? which is a nullable type or DateTime which is not nullable. The DateTime? can not be used with the method DateTime.TryParse because the 2nd parameter needs to be of type DateTime and not DateTime?.

Fernando
no more so i wanted him to know the method and check the documentation on it...I think its important to know what you're doing, know why you're doing it and also know to check the documentation and see how to use the item.  but as you saw, I still explained some of the usages of the item...since the 2nd post described an incorrect usage of DateTime.TryParse, my next few statements (posts 3 - 6) described DateTime.TryParse in more detail my initial post.