[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

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.
0
Rahn
Asked:
Rahn
  • 10
  • 4
1 Solution
 
silemoneCommented:
DateTime.TryParse
0
 
Reza RadCommented:
0
 
silemoneCommented:
DateTime.TryParse will try to parse a date value if possible...

works like this:
string date = "afdlsadf";
DateTime dateholder;
DateTime.TryParse(date, dateholder);
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
silemoneCommented:
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);
0
 
silemoneCommented:
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..
0
 
silemoneCommented:
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

0
 
Fernando SotoRetiredCommented:
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
0
 
silemoneCommented:
with that, i think I've given you a clear solution and fair explanation...if you have further questions, please ask...
0
 
silemoneCommented:
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.
0
 
Fernando SotoRetiredCommented:
@ 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
0
 
Fernando SotoRetiredCommented:
The message box is there to show how the code works and not necessarily to be coded that way.
0
 
silemoneCommented:
@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.
0
 
silemoneCommented:
@Fernando, gotcha on message box...now i understand better...
0
 
Fernando SotoRetiredCommented:
@ 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
0
 
silemoneCommented:
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.  
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 10
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now