开发者

How would I make a datetime into a specific custom format?

Say the current date is 1st Mar 2010, I want to display it like this...

20100301 so like first 4 digits = year, 2 digits = Month, 2 digits = day

is there an easy way to do 开发者_JAVA百科this?


use format

yourdatetimeObj.ToString("yyyyMMdd");

Ref: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx


Something like

dateTimeObject.ToString("yyyyMMdd");

See String Format for DateTime


var mydate = DateTime.Now; // Whatever you want.
mydate.ToString("yyyyMMdd");

Look at DateTimeFormatInfo for the other custom format strings you can use.


You can either use the ToString() implementation of the DateTime class, like the examples already given, or use a format string to display it along with other information, like so:

var now = DateTime.Now;
var msg = String.Format("Now: {0:dd/MM/yyyy}", now);

Or

Console.Write("Now: {0:MM/dd/yyyy}", now);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜