G
Guest
I want to convert a string to a DateTime type. The user can enter the date in
various formats and I am not sure if it will be a valid Date. Is it possible
to check if a string can be converted to a DateTime type without using a try
/ catch block. I was thinking of maybe something like is or as commands. But
both do not seem to work for this. Any suggestions will be much appreciated.
Right now i have :
string s = "01/05/2006"; //user will be entering the data ,
//this could be something
invalid like 22/22/2006
try
{
DateTime dt = DateTime.Parse(s);
MessageBox.Show("Date Parse Ok!");
//..other processing
}
catch(FormatException formatEx)
{
MessageBox.Show(formatEx.Message);
}
I was thinking of something along the lines :
if(s is DateTime)
MessageBox.Show("Date !!!");
else
MessageBox.Show("String...");
but this does not work as C# always considers s to be of type string.
Is there any way of not using the try / catch block? I am using .Net ver
1.1
Thanks in advance.
various formats and I am not sure if it will be a valid Date. Is it possible
to check if a string can be converted to a DateTime type without using a try
/ catch block. I was thinking of maybe something like is or as commands. But
both do not seem to work for this. Any suggestions will be much appreciated.
Right now i have :
string s = "01/05/2006"; //user will be entering the data ,
//this could be something
invalid like 22/22/2006
try
{
DateTime dt = DateTime.Parse(s);
MessageBox.Show("Date Parse Ok!");
//..other processing
}
catch(FormatException formatEx)
{
MessageBox.Show(formatEx.Message);
}
I was thinking of something along the lines :
if(s is DateTime)
MessageBox.Show("Date !!!");
else
MessageBox.Show("String...");
but this does not work as C# always considers s to be of type string.
Is there any way of not using the try / catch block? I am using .Net ver
1.1
Thanks in advance.