How do I convert a DateTime object to a string with just the date using C#?
Seemingly simple. I need to convert a DateTime object to a string without the time stamp.
This gives me the date and time. And I can't figure out how to just get the date.
startDate = Convert.ToString(beginningDate);
This outputs: 10/1/2011 12:00:00 AM
I need it to be: 10/1/201开发者_StackOverflow1 as a string
Any help is appreciated.
Check out .ToShortDateString()
string startDate = beginningDate.ToShortDateString();
Also check out the .ToString
method on DateTime
that takes a format string. For example:
string startDate = beginningDate.ToString("d");
Example: http://ideone.com/tpyZy
use
string startdate = beginningDate.ToShortDateString ();
For MSDN reference see http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx
you can do something like this;
DateTime dt = beginningDate; StringBuilder sbDate = Convert.toString(dt.year);//and further for others month and days
精彩评论