Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to date without time

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???*/
like image 537
Ognjen Avatar asked Dec 21 '09 00:12

Ognjen


1 Answers

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.

like image 166
Mark Byers Avatar answered Oct 07 '22 04:10

Mark Byers