Convert string to date without time

This line

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

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

after explaining i:

 DateTime dt = System.DateTime.Parse(Request.Form["datepicker"]); dt.ToString("dd/mm/yyyy"); /* and this code have time, why???*/ 
+6
c # asp.net-mvc
source share
3 answers

Your problem is not in parsing, but in the output. See how ToString works for DateTime or uses 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 get something in your locale:

 Console.WriteLine(dt.ToShortDateString()); 

Update. Your update to the question implies that you have not yet fully understood my answer, so I will add a little more explanation. There is no date in .NET - there is only DateTime. If you want to represent the date in .NET, you do this by storing the time of midnight at the beginning of this day. Time should always be kept, even if you do not need it. You cannot delete it. The important point is that when you show this DateTime to the user, you only show the Date part.

+21
source share

All elements processed by the DateTime object will contain date + time. If the time is not canceled, the estimated value will be 0000 hours.

To get a view for date only, it depends on how you format it for the string.

eg. theDate.ToString("dd/MM/yyyy")

Refer to the MSDN Date and Time Format Strings .

+4
source share

If you want to convert the datetime gridview column to columns by date only, use this code:

 raddgvDaybook.Rows.ToList().ForEach(x => { DateTime dt = DateTime.Parse(x.Cells["Vocdate"].Value.ToString()); string str = string.Format("{0:MM/dd/yyyy}", dt); x.Cells["Vocdate"].Value = str; }); 

I tested the code that will work if you bind datetime as a string in dgv.

0
source share

All Articles