The following example describes how to format Datetime in c# .net Framework. Here explain about custom datetime formatting and Standard Datetime formatting.
custom format specifiers y
(year), M
(month), d
(day), h
(hour 12), H
(hour 24),m
(minute), s
(second), f
(second fraction), F
(second fraction, trailing zeroes are trimmed), t
(P.M or A.M) and z
(time zone).
In DateTimeFormatInfo there are defined standard patterns for the current culture. For example property ShortTime Pattern isstring that contains value h:mm tt
for en-US culture and value HH:mm
for de-DE culture.
Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture. First column contains format specifiers for the String.Format method.
static void Main(string[] args)
{
try
{
DateTime dt = DateTime.Now;
StringBuilder str = new StringBuilder();
str.AppendLine(String.Format("{0:y yy yyy yyyy}", dt)); // "8 08 008 2008" year
str.AppendLine(String.Format("{0:M MM MMM MMMM}", dt)); // "3 03 Mar March" month
str.AppendLine(String.Format("{0:d dd ddd dddd}", dt)); // "9 09 Sun Sunday" day
str.AppendLine(String.Format("{0:h hh H HH}", dt)); // "4 04 16 16" hour 12/24
str.AppendLine(String.Format("{0:m mm}", dt)); // "5 05" minute
str.AppendLine(String.Format("{0:s ss}", dt)); // "7 07" second
str.AppendLine(String.Format("{0:f ff fff ffff}", dt)); // "1 12 123 1230" sec.fraction
str.AppendLine(String.Format("{0:F FF FFF FFFF}", dt)); // "1 12 123 123" without zeroes
str.AppendLine(String.Format("{0:t tt}", dt)); // "P PM" A.M. or P.M.
str.AppendLine(String.Format("{0:z zz zzz}", dt)); // "-6 -06 -06:00" time zone// date separator in germanculture is "." (so "/" changes to ".")
str.AppendLine(String.Format("{0:d/M/yyyy HH:mm:ss}", dt)); // "9/3/2008 16:05:07" -english (en-US)
str.AppendLine(String.Format("{0:d/M/yyyy HH:mm:ss}", dt)); // "9.3.2008 16:05:07" -german (de-DE)//-----examples of custom date andtime formatting-------//
// month/day numbers without/withleading zeroes
str.AppendLine(String.Format("{0:M/d/yyyy}", dt)); // "3/9/2008"
str.AppendLine(String.Format("{0:MM/dd/yyyy}", dt)); // "03/09/2008"
// day/month names
str.AppendLine(String.Format("{0:ddd, MMM d, yyyy}", dt)); // "Sun, Mar 9, 2008"
str.AppendLine(String.Format("{0:dddd, MMMM d, yyyy}", dt)); // "Sunday, March 9, 2008"// two/four digit year
str.AppendLine(String.Format("{0:MM/dd/yy}", dt)); // "03/09/08"
str.AppendLine(String.Format("{0:MM/dd/yyyy}", dt)); // "03/09/2008"//-----standard format specifiersin String.Format method -------//
str.AppendLine(String.Format("{0:t}", dt)); // "4:05 PM" ShortTime
str.AppendLine(String.Format("{0:d}", dt)); // "3/9/2008" ShortDate
str.AppendLine(String.Format("{0:T}", dt)); // "4:05:07 PM" LongTime
str.AppendLine(String.Format("{0:D}", dt)); // "Sunday, March 09, 2008" LongDate
str.AppendLine(String.Format("{0:f}", dt)); // "Sunday, March 09, 2008 4:05 PM" LongDate+ShortTime
str.AppendLine(String.Format("{0:F}", dt)); // "Sunday, March 09, 2008 4:05:07 PM"FullDateTime
str.AppendLine(String.Format("{0:g}", dt)); // "3/9/2008 4:05 PM" ShortDate+ShortTime
str.AppendLine(String.Format("{0:G}", dt)); // "3/9/2008 4:05:07 PM" ShortDate+LongTime
str.AppendLine(String.Format("{0:m}", dt)); // "March 09" MonthDay
str.AppendLine(String.Format("{0:y}", dt)); // "March, 2008" YearMonth
str.AppendLine(String.Format("{0:r}", dt)); // "Sun, 09 Mar 2008 16:05:07 GMT" RFC1123
str.AppendLine(String.Format("{0:s}", dt)); // "2008-03-09T16:05:07" SortableDateTime
str.AppendLine(String.Format("{0:u}", dt)); // "2008-03-09 16:05:07Z" UniversalSortableDateTimeConsole.WriteLine(str.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
output:
16 16 2016 2016 // year
3 03 Mar March // month
7 07 Mon Monday // day
9 09 21 21 // hour 12/24
22 22 // minute
33 33 // second
8 85 856 8562 sec.fraction
8 85 856 8562 // withoutzeroes
P PM // A.M. or P.M.
+5 +05 +05:30 // time zone
7/3/2016 21:22:33 // -english (en-US)
7/3/2016 21:22:33 // - german (de-DE)
3/7/2016
03/07/2016
Mon, Mar 7, 2016
Monday, March 7, 2016
03/07/16
03/07/2016
9:22 PM // ShortTime
3/7/2016 // ShortDate
9:22:33 PM // LongTime
Monday, March 07, 2016 // LongDate
Monday, March 07, 2016 9:22 PM // LongDate+ShortTime
Monday, March 07, 2016 9:22:33 PM // FullDateTime
3/7/2016 9:22 PM // ShortDate+ShortTime
3/7/2016 9:22:33 PM // ShortDate+LongTime
March 07 MonthDay
March, 2016 // YearMonth
Mon, 07 Mar 2016 21:22:33 GMT //RFC1123
2016-03-07T21:22:33 // SortableDateTime
2016-03-07 21:22:33Z // UniversalSortableDateTime
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article