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,
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);
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With