C# - DateTime To String

String _display = DateTime.Now.ToString("dd/MM/yyyy");
_display result is: 27/12/2011

You can display by .NET Datetime format characters as the following:

d - Numeric day of the month without a leading zero.
dd - Numeric day of the month with a leading zero.
ddd - Abbreviated name of the day of the week.
dddd - Full name of the day of the week.

f,ff,fff,ffff,fffff,ffffff - Fraction of a second. The more Fs the higher the precision.

h - 12 Hour clock, no leading zero.
hh - 12 Hour clock with leading zero.
H - 24 Hour clock, no leading zero.
HH - 24 Hour clock with leading zero.

m - Minutes with no leading zero.
mm - Minutes with leading zero.

M - Numeric month with no leading zero ex.2.
MM - Numeric month with a leading zero ex.02.
MMM - Abbreviated name of month ex.Dec
MMMM - Full month name ex.December.

s - Seconds with no leading zero.
ss - Seconds with leading zero.

t - AM/PM but only the first letter.
tt - AM/PM ( a.m. / p.m.)

y - Year with out century and leading zero.
yy - Year with out century, with leading zero.
yyyy - Year with century.

zz - Time zone off set with +/-.

Example:

String _display = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss tt");
_display result is: 27/12/2011 17:23:11 PM


web reference: Code project

Popular posts from this blog

SAP CPI - Loop Process Call with SuccessFactor (oData V2)

Setting IntelliJ IDEA to run Groovy Script

C# - BASE64 to Image