Remove the time part and convert date time in different format
Solution 1
There in no convertion in datetime. If you have a string to datetime, use a DateTime.TryParse. If you have a datetime and want a formatted string. You can use a format string:
dateAndTime.ToString("MMMM dd,yyyy");
will give you a formatted date string.
Solution 2
Use following to get format October 23, 2013
dt.ToString("MMMM dd, yyyy");
Solution 3
The DateTime.Date
is just a DateTime
with the time portion set to zero, or midnight. If you are trying to output it as a string, then you can use either ToShortDateString()
or one of the many various custom formats.
date.ToString("MMMM dd,yyyy")
should give you the output you are looking for.
For information on DateTime.Date
http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx
For more information on format strings for DateTime
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Solution 4
Here are all the standard date formats.
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
Solution 5
Use the Date property:
OR
var dateAndTime = DateTime.Now;
dateAndTime.ToString("dd/MM/yyyy")
The date variable will contain the date, the time part will be 00:00:00.
ankur
Updated on July 09, 2022Comments
-
ankur almost 2 years
I have a date time object ,Whose time part I am extracting by this
var dateAndTime = Convert.ToDateTime(certificate.GetEffectiveDateString()); var date = dateAndTime.Date;
date is returning a value :
10/23/2013 12:00:00 AM
where is should only return10/23/2013
Don't know why the time part is not removed.After time part is removed, I need date to be converted in to this format
October 23,2013
.Can we remove the time part and change it into this format by some formats defined.
-
CodeCaster over 10 yearspossible duplicate of Display only date and no time
-