| | IntroductionThis code snippet, I have described different ways of DateTime formatting in C#. Because every application users, they would like to have date and time different way or format. Here is list of supporting DateTime format strings http://msdn.microsoft.com/en-us/library/az4se3k1%28VS.71%29.aspx Implementationusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DateTimeDemo
{class Program
{static void Main(string[] args)
{
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "Char In use ", "Description ", "Out put"));
Console.WriteLine(string.Format("{0}", "---------------------------------------------------"));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "d", "Short Format", DateTime.Now.ToString("d")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "D", "Long Format", DateTime.Now.ToString("D")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "T", "Long Time", DateTime.Now.ToString("T")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "t", "short Time", DateTime.Now.ToString("t")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "f", "Full Date and time ", DateTime.Now.ToString("f")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "F", "Full date and Time-Long", DateTime.Now.ToString("F")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "G", "Default date and time -Long", DateTime.Now.ToString("G")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "g", "Default date and time", DateTime.Now.ToString("g")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "M", "Day / Month", DateTime.Now.ToString("M")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "r", "RFC1123 date", DateTime.Now.ToString("r")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "s ", "Sortable date/time", DateTime.Now.ToString("s")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "u ", "Universal time, local timezone ", DateTime.Now.ToString("u")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "Y ", " Month / Year ", DateTime.Now.ToString("Y")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "ss ", "seconds ", DateTime.Now.ToString("ss")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "tt ", "AM/PM ", DateTime.Now.ToString("tt")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "hh ", "2 digit hour ", DateTime.Now.ToString("hh")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "HH ", "2 digit hour (24 hour)", DateTime.Now.ToString("HH")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "mm ", "2 digit minute ", DateTime.Now.ToString("mm")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "dd ", "Day 08", DateTime.Now.ToString("dd")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "ddd ", "Short Day Name ", DateTime.Now.ToString("ddd")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "dddd ", "Full Day Name ", DateTime.Now.ToString("dddd")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "MM ", "Month ", DateTime.Now.ToString("MM")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "MMM ", "Short Month name ", DateTime.Now.ToString("MMM")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "MMMM ", "Month name ", DateTime.Now.ToString("MMMM")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "yy ", "2 digit year ", DateTime.Now.ToString("yy")));
Console.WriteLine(string.Format("{0}\t{1}\t{2}", "yyyy", " 4 digit year ", DateTime.Now.ToString("yyyy")));
Console.ReadLine();
}
}
}
OutputT Long Time 4:40:26 PM
t short Time 4:40 PM
f Full Date and time Saturday, July 03, 2010 4:40 PM
F Full date and Time-Long Saturday, July 03, 2010 4:40:26 PM
G Default date and time -Long 7/3/2010 4:40:26 PM
g Default date and time 7/3/2010 4:40 PM
M Day / Month July 03
r RFC1123 date Sat, 03 Jul 2010 16:40:26 GMT
s Sortable date/time 2010-07-03T16:40:26
u Universal time, local timezone 2010-07-03 16:40:26Z
Y Month / Year July, 2010
ss seconds 26
tt AM/PM PM
hh 2 digit hour 04
HH 2 digit hour (24 hour) 16
mm 2 digit minute 40
dd Day 08 03
ddd Short Day Name Sat
dddd Full Day Name Saturday
MM Month 07
MMM Short Month name Jul
MMMM Month name July
yy 2 digit year 10
yyyy 4 digit year 2010
That's all, i hope this is save your time.thank you for reading. | |