Convetting string/text to datetime

  • Thread starter Thread starter Randel Bjorkquist
  • Start date Start date
R

Randel Bjorkquist

I'm extremely new to C# and the Microsoft Visual Studio IDE. I'm writing a
small web-application that executes a SQL Server stored procedure. For the
most part, I have it working. I am at the point where I now just need to
pass two more fields to the stored procedure, they are both to be date
values.

I have on my web form, two TextBoxes that I am pulling the value from.
However, I have not found a way to convert a string, or text value, to a
date value. What I have found is a DateTime class, but all of its
constructors take numeric values to its parameters and not text. I looked
under the "string" class but didn't see any methods that would help me.

Can someone point me into the right direction? Any small, easy example
would be helpfull.

Thanks,

Randel Bjorkquist
 
I'm extremely new to C# and the Microsoft Visual Studio IDE. I'm writing a
small web-application that executes a SQL Server stored procedure. For the
most part, I have it working. I am at the point where I now just need to
pass two more fields to the stored procedure, they are both to be date
values.

I have on my web form, two TextBoxes that I am pulling the value from.
However, I have not found a way to convert a string, or text value, to a
date value. What I have found is a DateTime class, but all of its
constructors take numeric values to its parameters and not text. I looked
under the "string" class but didn't see any methods that would help me.

Can someone point me into the right direction? Any small, easy example
would be helpfull.

Thanks,

Randel Bjorkquist

DateTime.Parse(...) will do the trick! Youcan also use
Convert.ToDateTime(...).
 
Hi Ludwig,

I keep forgetting about the "parse" method.

Thanks for your help,

Randel Bjorkquist
 
Hi,

You could use DateTime.Parse or DateTime.ParseExact , a couple of remarks
though.
1- They throw exceptions if the string is not in the correct format, so you
better use a try block
2- It's a good idea to use also a Calendar web control, this makes it easier
for the users to enter values
3- Use page validators in the textboxes, to make sure that the text is in
fact a correct date expression.


hope this help,
 
Hey Ignacio,

Thanks for the help. I had just started looking into managing the
exceptions these might throw, when I checked back and saw your message.

Again, thanks,

Randel Bjorkquist
 
Back
Top