How to remove time portion of date in C# in DateTime object only?
Solution 1:
Use the Date property:
var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;
The date
variable will contain the date, the time part will be 00:00:00
.
Solution 2:
You can use format strings to give the output string the format you like.
DateTime dateAndTime = DateTime.Now;
Console.WriteLine(dateAndTime.ToString("dd/MM/yyyy")); // Will give you smth like 25/05/2011
Read more about Custom date and time format strings.
Solution 3:
Use the method ToShortDateString. See the documentation http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx
var dateTimeNow = DateTime.Now; // Return 00/00/0000 00:00:00
var dateOnlyString = dateTimeNow.ToShortDateString(); //Return 00/00/0000