tags:

views:

155

answers:

3

This line

System.DateTime.Parse("09/12/2009");

convert date (string) to 9/12/2009 12:00:00 AM. How can I get a date in the form 9/12/2009.

after explanations I do:

        DateTime dt = System.DateTime.Parse(Request.Form["datepicker"]);
        dt.ToString("dd/mm/yyyy");
        /* and this code have time, why???*/
+9  A: 

Your problem is not with the parsing but with the output. Look at how ToString works for DateTime, or use this example:

using System;

class Program
{
    static void Main(string[] args)
    {
        DateTime dt = DateTime.Parse("09/12/2009");
        Console.WriteLine(dt.ToString("dd/MM/yyyy"));
    }
}

Or to get something in your locale:

Console.WriteLine(dt.ToShortDateString());

Update: Your update to the question implies that you do not understand fully my answer yet, so I'll add a little more explanation. There is no Date in .NET - there is only DateTime. If you want to represent a date in .NET, you do so by storing the time midnight at the start of that day. The time must always be stored, even if you don't need it. You cannot remove it. The important point is that when you display this DateTime to a user, you only show them the Date part.

Mark Byers
+1 Here is the best list I have found, I've had this bookmarked for years, of format overrides.http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm
Clarence Klopfstein
@Clarence: Good list indeed!
o.k.w
That's the list I linked the word ToString to. :)
Mark Byers
Ok, adding that to my bookmark! +1
o.k.w
So it is Mark. I totally overlooked that :-)
Clarence Klopfstein
+2  A: 

Anything parsed to a DateTime object will contain date+time. If no time is sepcified, the assumed value will be 0000hr.

To get the date-only representation, it's up to how you format it to string.

E.g. theDate.ToString("dd/MM/yyyy")

Refer to the MSDN Date and Time Format Strings.

o.k.w
A: 

Refer this link : Click me
for any more help you want with the date(date-time) formatting .. :)

infant programmer