C# .NET - Declaring a datetime variable - Asked By Michael Parker on 22-Nov-06 12:21 PM

So simple, but not in my grasp.

I want to declare a dateTime variable.

[code]

DateTime startDate = (1992,06,09)

[/code]

But can't find the syntax for this anywhere. Syntax for every other thing in the planent, but not for simple declaration.


Anyone can help?

Can declare as - Shallu Gupta replied to Michael Parker on 22-Nov-06 12:27 PM

DateTime dt = new DateTime(2006,09,23)

thank you -- is there a way to change the format? to YYYY-MM-DD ? - Michael Parker replied to Shallu Gupta on 22-Nov-06 12:30 PM

I am querying a table in SQL SErver, and it isn't working using the format that is above.
Sushila Patel replied to Michael Parker on 22-Nov-06 12:47 PM

DateTime dt = DateTime.Parse("<yourdate>");

Response.Write(dt.ToString("yyyy-MM-dd");

will give format of yyy-MM-dd

http://p2p.wrox.com/topic.asp?TOPIC_ID=17750 try this link.... - abc V replied to Michael Parker on 22-Nov-06 12:48 PM

DateTime dt = (DateTime) e.Value;
   string s = dt.ToString("dd/MM/yyyy");
   e.Value = s;

http://p2p.wrox.com/topic.asp?TOPIC_ID=17750

The thing is that i need a date in that format, not a string. My code will not take a string
Michael Parker replied to abc V on 22-Nov-06 01:58 PM
it needs a date.
DateTime is a structure, not a "format" - Peter Bromberg replied to Michael Parker on 22-Nov-06 02:11 PM

It has a number of methods (such as Parse, as you have already seen), and it also overloads the ToString method so that you can output it as a string in different formats.

Other methods are also overloaded.

The bottom line is that if you want a DateTime object, you can't have a string, you can only output it as a string in various formats.

Take a look at the documentation, it may help:

http://msdn2.microsoft.com/en-US/library/system.datetime(VS.80).aspx