开发者

How do I convert a DateTime object to a string with just the date using C#?

开发者 https://www.devze.com 2023-04-09 16:26 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号