how to convert datetime to short date?
Solution 1
You must call .ToString()
method with that format that you want. In your example it will be like this.
DateTime date=DateTime.Now;
var shortDate=date.ToString("yyyy-MM-dd");
Or if you want to keep DateTime
type, you must call .Date
property of DateTime
DateTime date=DateTime.Now;
var shortDate=date.Date;
From MSDN
A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
Solution 2
DateTime dateToDisplay = new DateTime(2014, 11, 03, 42, 50);
String shortDateTime = dateToDisplay.ToShortDateString();
Then you can convert this shortDateTime to datetime like this
DateTime shortDate = Convert.ToDateTime(shortDateTime);
Solution 3
This works good as well:
DateTime date = Convert.ToDateTime(item.BirthDate);
item.BirthDate = date.ToShortDateString();
Solution 4
Use following :
date.ToString("yyyy-MM-dd");
Solution 5
I was taking DateTime from Database as string. converted it to DateTime and I cutted the time from it. Here is what I have done.
DateTime date = Convert.ToDateTime(item.BirthDate);
item.BirthDate = date.ToString("dd/MM/yyyy");
aamankhaan
Updated on November 15, 2020Comments
-
aamankhaan over 3 years
i have a table called as X and this table has a field known as X_DateTime respectively, which has value like 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 remove the Time (09:26:37.837)
i have used something like below code
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 am using C# as my language.
thanks,