Date and Time Formatting Characters - CSharp Language Basics

CSharp examples for Language Basics:Data Type Format

Introduction

Specifier
Description
Default Format
Example Output
d
Short date
mm/dd/yyyy
5/6/2001
D
Long date
day, month dd, yyyy
Sunday, May 06, 2001
f

Full date/
short time
day, month dd,
yyyy hh:mm AM/PM
Sunday, May 06, 2001 12:30 PM

F

Full date/
full time
day, month dd,
yyyy HH:mm:ss AM/PM
Sunday, May 06, 2001 12:30:54 PM

g

Short date/
short time
mm/dd/yyyy HH:mm

6/5/2001 12:30 PM

G

Short date/
long time
mm/dd/yyyy hh:mm:ss

6/5/2001 12:30:54 PM

M or m
Month day
month dd
May 06
R or r

RFC1123

ddd, dd Month yyyy
hh:mm:ss GMT
Sun, 06 May 2001 12:30:54 GMT

s
Sortable
yyyy-mm-dd hh:mm:ss
2001-05-06T12:30:54
t
Short time
hh:mm AM/PM
12:30 PM
T
Long time
hh:mm:ss AM/PM
12:30:54 PM
u

Sortable
(universal)
yyyy-mm-dd hh:mm:ss

2001-05-06 12:30:54Z

U

Sortable
(universal)
day, month dd, yyyy
hh:mm:ss AM/PM
Sunday, May 06, 2001 12:30:54 PM

Y or y
Year/month
month, yyyy
May, 2001

Demo Code

using System;/*from  ww  w  .j  a  v  a  2s  .c om*/
class DtFormat
{
   public static void Main()
   {
      DateTime CurrTime = DateTime.Now;
      Console.WriteLine("d: {0:d}", CurrTime );
      Console.WriteLine("D: {0:D}", CurrTime );
      Console.WriteLine("f: {0:f}", CurrTime );
      Console.WriteLine("F: {0:F}", CurrTime );
      Console.WriteLine("g: {0:g}", CurrTime );
      Console.WriteLine("G: {0:G}", CurrTime );
      Console.WriteLine("m: {0:m}", CurrTime );
      Console.WriteLine("M: {0:M}", CurrTime );
      Console.WriteLine("r: {0:r}", CurrTime );
      Console.WriteLine("R: {0:R}", CurrTime );
      Console.WriteLine("s: {0:s}", CurrTime );
      Console.WriteLine("t: {0:t}", CurrTime );
      Console.WriteLine("T: {0:T}", CurrTime );
      Console.WriteLine("u: {0:u}", CurrTime );
      Console.WriteLine("U: {0:U}", CurrTime );
      Console.WriteLine("y: {0:y}", CurrTime );
      Console.WriteLine("Y: {0:Y}", CurrTime );
   }
}

Result


Related Tutorials