;

String Format for DateTime in C#


Tutorialsrack 16/06/2020 C#

In this article, you will learn how to format DateTime using the String format method in C#. For formatting the DateTime, we used the String.Format() method of the String class. 

Custom date and time format strings

The following table describes the custom date and time format specifiers and displays a result string produced by each format specifier. By default, result strings reflect the formatting conventions of the en-US culture. 

Format Specifier Description
"d" The day of the month, from 1 through 31.
"dd" The day of the month, from 01 through 31.
"ddd" The abbreviated name of the day of the week.
"dddd" The full name of the day of the week.
"f" The tenths of a second in a date and time value.
"ff" The hundredths of a second in a date and time value.
"fff" The milliseconds in a date and time value.
"ffff" The ten-thousandths of a second in a date and time value.
   
"fffff" The hundred-thousandths of a second in a date and time value.
"ffffff" The millionths of a second in a date and time value.
"fffffff" The ten-millionths of a second in a date and time value.
"F" If non-zero, the tenths of a second in a date and time value.
"FF" If non-zero, the hundredths of a second in a date and time value.
"FFF" If non-zero, the milliseconds in a date and time value.
"FFFF" If non-zero, the ten-thousandths of a second in a date and time value.
"FFFFF" If non-zero, the hundred-thousandths of a second in a date and time value.
"FFFFFF" If non-zero, the millionths of a second in a date and time value.
"FFFFFFF" If non-zero, the ten-millionths of a second in a date and time value.
"g", "gg" The period or era.
"h" The hour, using a 12-hour clock from 1 to 12.
"hh" The hour, using a 12-hour clock from 01 to 12.
"H" The hour, using a 24-hour clock from 0 to 23.
"HH" The hour, using a 24-hour clock from 00 to 23.
"K" Time zone information.
"m" The minute, from 0 through 59.
"mm" The minute, from 00 through 59.
"M" The month, from 1 through 12.
"MM" The month, from 01 through 12.
"MMM" The abbreviated name of the month.
"MMMM" The full name of the month.
"s" The second, from 0 through 59.
"ss" The second, from 00 through 59.
"t" The first character of the AM/PM designator.
"tt" The AM/PM designator.
"y" The year, from 0 to 99.
"yy" The year, from 00 to 99.
"yyy" The year, with a minimum of three digits.
"yyyy" The year as a four-digit number.
"yyyyy" The year as a five-digit number.
"z" Hours offset from UTC, with no leading zeros.
"zz" Hours offset from UTC, with a leading zero for a single-digit value.
"zzz" Hours and minutes offset from UTC.
":" The time separator.
"/" The date separator.

"string"

'string'
Literal string delimiter.
% Defines the following character as a custom format specifier.
\ The escape character.
Any other character The character is copied to the result string unchanged

Here is the Example of Custom Datetime Format using String.Format() method in C#.

Example 1: Custom Formatting Date and Time Using String.Format() Method
using System;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Format DateTime Using String.Format() Method in C# */
        static void Main(string[] args)
        {
            // create date time 2006-05-08 21:05:07.123
            DateTime date = new DateTime(2006, 05, 08, 21, 5, 7, 123);

            Console.WriteLine(String.Format("{0:y yy yyy yyyy}", date));
            // Output ==> "6 06 006 2006"   year

            Console.WriteLine(String.Format("{0:M MM MMM MMMM}", date));
            // Output ==> "5 05 May May"  month

            Console.WriteLine(String.Format("{0:d dd ddd dddd}", date));
            // Output ==> "8 08 Mon Monday" day

            Console.WriteLine(String.Format("{0:h hh H HH}", date));
            // Output ==> "9 09 21 21"      hour 12/24

            Console.WriteLine(String.Format("{0:m mm}", date));
            // Output ==> "5 05"            minute

            Console.WriteLine(String.Format("{0:s ss}", date));
            // Output ==> "7 07"            second

            Console.WriteLine(String.Format("{0:f ff fff ffff}", date));
            // Output ==> "1 12 123 1230"   sec.fraction

            Console.WriteLine(String.Format("{0:F FF FFF FFFF}", date));
            // Output ==> "1 12 123 123"    without zeroes

            Console.WriteLine(String.Format("{0:t tt}", date));
            //Output ==> "P PM"            A.M. or P.M.

            Console.WriteLine(String.Format("{0:z zz zzz}", date));
            // Output ==> "+5 +05 +05:30"   time zone

            //Hit ENTER to exit the program
            Console.ReadKey();
        }
    }
}

You can also use Date separator slash (/) Operator and Time separator colon (:) operator to custom DateTime format using String.Format().

Here is the example of Date and Time with Custom Format using Separator.

Example 2: Custom Formatting Date and Time
Console.WriteLine(String.Format("{0:dd/MM/yyyy hh:mm:ss tt}", date));
// Output ==> "08-05-2006 09:05:07 PM" - english (en-US)

// Date Separator in Turkish culture is "." 
// So Replace "/" with "." to print Turkish Date
Console.WriteLine(String.Format("{0:dd.MM.yyyy hh:mm:ss tt}", date));
// Output ==> "08.05.2006 09:05:07 PM" - Turkish (tr-TR)

Here are some examples of custom date and time formatting using String.Format() Method.

Example 3: Custom Formatting of Date and Time
            // Examples of Custom Date Time Formatting

            Console.WriteLine(String.Format("{0:dd/MM/yyyy hh:mm:ss tt}", date));
            // Output ==> "08-05-2006 09:05:07 PM"

            Console.WriteLine(String.Format("{0:MM/dd/yyyy hh:mm:ss tt}", date));
            // Output ==> "05-08-2006 09:05:07 PM"

            Console.WriteLine(String.Format("{0:MM/dd/yy hh:mm:ss tt}", date));
            // Output ==> "05-08-06 09:05:07 PM"

            Console.WriteLine(String.Format("{0:dd.MM.yyyy}", date));
            // Output ==> "08.05.2006"

            Console.WriteLine(String.Format("{0:d/M/yy hh:mm:ss tt}", date));
            // Output ==> "8-5-06 09:05:07 PM"

            Console.WriteLine(String.Format("{0:ddd, MMM dd, yyyy hh:mm:ss tt}", date));
            // Output ==> "Mon, May 08, 2006 09:05:07 PM"

            Console.WriteLine(String.Format("{0:dddd, MMM dd, yyyy hh:mm:ss tt}", date));
            // Output ==> "Monday, May 08, 2006 09:05:07 PM"

            Console.WriteLine(String.Format("{0:MMM dd, yyyy hh:mm:ss tt}", date));
            // Output ==> "May 08, 2006 09:05:07 PM"

            Console.WriteLine(String.Format("{0:dd-MMM-yyyy hh:mm:ss tt}", date));
            // Output ==> "08-May-2006 09:05:07 PM"

            Console.WriteLine(String.Format("{0:dd/MMM/yyyy}", date));
            // Output ==> "08-May-2006"

Standard DateTime Formatting

A standard Date and Time format string use a single format specifier to define the text representation of a date and time value. 

The following table describes the standard date and time format specifiers and their values for en-US culture. The first column contains format specifiers for the String.Format() method.

# Format Specifier Description Pattern Values
1 "d" Short date pattern. M/d/yyyy
2 "D" Long date pattern. dddd, MMMM dd, yyyy
3 "f" Full date/time pattern (short time). dddd, MMMM dd, yyyy h:mm tt
4 "F" Full date/time pattern (long time). dddd, MMMM dd, yyyy h:mm:ss tt
5 "g" General date/time pattern (short time). M/d/yyyy h:mm tt
6 "G" General date/time pattern (long time). M/d/yyyy h:mm:ss tt
7 "M", "m" Month/day pattern. MMMM dd
8 "O", "o" Round-trip date/time pattern.

Datetime Values: 

yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK

DateTimeOffset Values: 

yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz

9 "R", "r" RFC1123 pattern. ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*)
10 "s" Sortable date/time pattern. yyyy'-'MM'-'dd'T'HH':'mm':'ss (*)
11 "t" Short time pattern. h:mm tt
12 "T" Long time pattern. h:mm:ss tt
13 "u" Universal sortable date/time pattern.

yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*)

(*) = culture independent

14 "U" Universal full date/time pattern.

dddd, MMMM dd, yyyy HH:mm:ss

15 "Y", "y" Year month pattern.

MMMM, yyyy

  Any other single character Unknown specifier.

Throws a run-time FormatException.

Here are some examples of Standard date and time formatting using String.Format() Method.

Example 4: Standard Date and Time Formatting usinfgString.Format() Method
using System;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Format DateTime Using String.Format() Method in C# */
        static void Main(string[] args)
        {
            // create date time 2020-05-08 21:05:07.123
            DateTime date = new DateTime(2020, 05, 08, 21, 5, 7, 123);
            
            Console.WriteLine(String.Format("{0:t}", date));
            // Output ==> "9:05 PM"                        ShortTime

            Console.WriteLine(String.Format("{0:d}", date));
            // Output ==> "5/8/2020"                       ShortDate

            Console.WriteLine(String.Format("{0:T}", date));
            // Output ==> "9:05:07 PM"                     LongTime

            Console.WriteLine(String.Format("{0:D}", date));
            // Output ==> "Friday, May 8, 2020"            LongDate
            Console.WriteLine(String.Format("{0:f}", date));
            // Output ==> "Friday, May 8, 2020 9:05 PM"    LongDate+ShortTime

            Console.WriteLine(String.Format("{0:F}", date));
            // Output ==> "Friday, May 8, 2020 9:05:07 PM" FullDateTime

            Console.WriteLine(String.Format("{0:g}", date));
            // Output ==> "5/8/2020 9:05 PM"               ShortDate+ShortTime

            Console.WriteLine(String.Format("{0:G}", date));
            // Output ==> "5/8/2020 9:05:07 PM"            ShortDate+LongTime

            Console.WriteLine(String.Format("{0:m}", date));
            // Output ==> "May 8"                          MonthDay

            Console.WriteLine(String.Format("{0:y}", date));
            // Output ==> "May 2020"                       YearMonth

            Console.WriteLine(String.Format("{0:r}", date));
            // Output ==> "Fri, 08 May 2020 21:05:07 GMT"   RFC1123

            Console.WriteLine(String.Format("{0:o}", date));
            // Output ==> "2020-05-08T21:05:07.1230000"     Round-trip date/time 

            Console.WriteLine(String.Format("{0:s}", date));
            // Output ==> "2020-05-08T21:05:07"             SortableDateTime

            Console.WriteLine(String.Format("{0:u}", date));
            // Output ==> "2020-05-08 21:05:07Z"            UniversalSortableDateTime

            //Hit ENTER to exit the program
            Console.ReadKey();
        }
    }
}

I hope this article will help you to understand how to Custom Format Date and Time and How to Format Date and Time in Standard Date and Time Format using String.Format() Method in C#

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!

 


Related Posts



Comments

Recent Posts
Tags