How could I make datetime in a specific user format?

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

20100301 as the first 4 digits = year, 2 digits = month, 2 digits = day

is there an easy way to do this?

+5
source share
4 answers

use format

yourdatetimeObj.ToString("yyyyMMdd");

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

+6
source

Sort of

dateTimeObject.ToString("yyyyMMdd");

See String Format for DateTime

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

DateTimeFormatInfo , .

+2

ToString() DateTime, , , , , :

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

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

All Articles