date Time variable in c#

G

Guest

how do I decalre a variable as a datetime datatype in c#

for example a variable called 'x'

i've tried private DateTime x,
and i've tried system.datetime x

but none seem to work

does anyone have the solution

much appreciated
 
B

Brock Allen

C# is case sensitive:

System.DateTime x;

or you there's a "using System;" at the top, you can just use

using System;

class Foo
{
void Bar()
{
DateTime x;
}
}
 
K

Karl Seguin

What doesn't work?

both should work....do you want to intialize it to a particular date as
well?

Karl
 
G

Guest

What im trying to do is declare a variable(startDate) as a datetime, this
variable will be the inputted by a user at runtime into a textbox as a date.
Then that value of textbox will be inserted to a column in a sql database
which has a datetime column.
 
S

Siva M

Not sure if this is what you are looking for:

DateTime x;
x = DateTime.Parse(TextBox.Text); // or use DateTime.ParseExact() if you
have fixed format
// Use x now to insert into the database table.

What im trying to do is declare a variable(startDate) as a datetime, this
variable will be the inputted by a user at runtime into a textbox as a date.
Then that value of textbox will be inserted to a column in a sql database
which has a datetime column.
 
K

Karl Seguin

Just note that DateTime.Parse() might throw an exception if it can't
parse...ie if the user entered "aaa" so either validate or use a try catch
and catch for the specific exceptions..

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top