How to convert datetime to short date?

I have a table called X, and this table has a field known as X_DateTime respectively, which has a value similar to 2012-03-11 09: 26: 37.837.

I want to convert the above datetime value to this format yyyy-MM-dd (2012-03-11) and literally delete Time (09: 26: 37.837)

I used something like the code below

DateTimeFormatInfo format = new DateTimeFormatInfo();
format.ShortDatePattern = "yyyy-MM-dd";
format.DateSeparator = "-";
DateTime date = 2012-03-11 09:26:37.837;
DateTime shortDate = Convert.ToDateTime(date , format);

Please help me...

I use C # as my language.

thank,

+5
source share
6 answers

You must call the method .ToString()in the format you want. In your example, this will be so.

DateTime date=DateTime.Now;
var shortDate=date.ToString("yyyy-MM-dd");

Or, if you want to save the type DateTime, you must call the property.Date DateTime

DateTime date=DateTime.Now;
var shortDate=date.Date;

From MSDN

, , 12:00:00 (00:00:00).

+12
DateTime dateToDisplay = new DateTime(2014, 11, 03, 42, 50);
String shortDateTime = dateToDisplay.ToShortDateString();

shortDateTime datetime,

DateTime shortDate = Convert.ToDateTime(shortDateTime);
+3

:

DateTime date = Convert.ToDateTime(item.BirthDate);
item.BirthDate = date.ToShortDateString();
+2

:

date.ToString("yyyy-MM-dd");
+1
source

I took a DateTime from the database as a string. converted it to DateTime, and I cut the time from it. Here is what I did.

DateTime date = Convert.ToDateTime(item.BirthDate);
item.BirthDate = date.ToString("dd/MM/yyyy");
0
source
DateTimeFormatInfo format = new DateTimeFormatInfo(); 
format.ShortDatePattern = "yyyy-MM-dd"; 
format.DateSeparator = "-"; 
DateTime date = 2012-03-11 09:26:37.837; 
DateTime shortDate = Convert.ToDateTime(date , format);
0
source

All Articles