DateTime Format In C# - C# Tutorial And Source Code

C# DateTime Format By: Rajesh P.S.

A date and time format string defines the text representation of a DateTime value that results from a formatting operation . C# includes a really great struct for working with dates and time.

DateTime dt = new DateTime(); DateTime dt = new DateTime(2000, 1, 10);

You can assign the DateTime object a date and time value returned by a property or method.

DateTime dt1 = DateTime.Now; //returns current date and time DateTime dt2 = DateTime.Today; // returns today's date

Standard date and time format strings

A standard date and time format string in C# uses a single format specifier to define the text representation of a date and time value. A standard or custom format string can be used in two different ways:

  1. To define the string that results from a formatting operation.
  2. To define the text representation of a date and time value that can be converted to a DateTime value by a parsing operation.

Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture.

Date Formatting in C#

Standard formats are typically used when you need a fast string representation of your DateTime object based on current culture.

C# Standard date and time Example using System; namespace CStandardDateTime { class Program { static void Main(string[] args) { DateTime dt = DateTime.Now; Console.WriteLine(String.Format("{0:t}", dt) ); // ShortTime Console.WriteLine(String.Format("{0:d}", dt) ); // ShortDate Console.WriteLine(String.Format("{0:T}", dt) ); // LongTime Console.WriteLine(String.Format("{0:D}", dt) ); // LongDate Console.WriteLine(String.Format("{0:f}", dt) ); // LongDate+ShortTime Console.WriteLine(String.Format("{0:F}", dt) ); // FullDateTime Console.WriteLine(String.Format("{0:g}", dt) ); // ShortDate+ShortTime Console.WriteLine(String.Format("{0:G}", dt) ); // ShortDate+LongTime Console.WriteLine(String.Format("{0:m}", dt) ); // MonthDay Console.WriteLine(String.Format("{0:y}", dt) ); // YearMonth Console.WriteLine(String.Format("{0:r}", dt) ); // RFC1123 Console.WriteLine(String.Format("{0:s}", dt) ); // SortableDateTime Console.WriteLine(String.Format("{0:u}", dt) ); // UniversalSortableDateTime Console.ReadKey(); } } } output C# Custom date and time format strings

Custom date and time format string

A custom date and time format string consists of two or more characters. Date and time formatting methods interpret any single-character string as a standard date and time format string. In formatting operations , custom date and time format strings can be used either with the ToString method of a date and time instance or with a method that supports composite formatting .

DateTime dt = new DateTime((2000, 1, 10) Console.WriteLine("The date is " + dt.ToString("MMMM dd, yyyy") + "."); output The date is January 10, 2000.

The following table describes various C# DateTime formats .

Format
DateTime.Now.ToString("MM/dd/yyyy")
DateTime.Now.ToString("HH:mm")
DateTime.Now.ToString("hh:mm tt")
DateTime.Now.ToString("H:mm")
DateTime.Now.ToString("h:mm tt")
DateTime.Now.ToString("HH:mm:ss")
DateTime.Now.ToString("dddd, dd MMMM yyyy")
DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")
DateTime.Now.ToString("MM/dd/yyyy HH:mm")
DateTime.Now.ToString("MM/dd/yyyy hh:mm tt")
DateTime.Now.ToString("MM/dd/yyyy H:mm")
DateTime.Now.ToString("MM/dd/yyyy h:mm tt")
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")
DateTime.Now.ToString("MMMM dd")
DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK")
DateTime.Now.ToString("ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’")
DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss")
DateTime.Now.ToString("yyyy MMMM")
C# DateTime Example using System; namespace CustomDateTime { class Program { static void Main(string[] args) { DateTime dt = new DateTime(2000, 1, 10); // Format Datetime in different formats and display them Console.WriteLine(dt.ToString("MM/dd/yyyy")); Console.WriteLine(dt.ToString("HH:mm")); Console.WriteLine(dt.ToString("hh:mm tt")); Console.WriteLine(dt.ToString("H:mm")); Console.WriteLine(dt.ToString("h:mm tt")); Console.WriteLine(dt.ToString("HH:mm:ss")); Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm:ss")); Console.WriteLine(dt.ToString("MM/dd/yyyy HH:mm")); Console.WriteLine(dt.ToString("MM/dd/yyyy hh:mm tt")); Console.WriteLine(dt.ToString("MM/dd/yyyy H:mm")); Console.WriteLine(dt.ToString("MM/dd/yyyy h:mm tt")); Console.WriteLine(dt.ToString("MM/dd/yyyy HH:mm:ss")); Console.WriteLine(dt.ToString("MMMM dd")); Console.WriteLine(dt.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK")); Console.WriteLine(dt.ToString("ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’")); Console.WriteLine(dt.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss")); Console.WriteLine(dt.ToString("yyyy MMMM")); } } } output Difference Between Task and Thread Related Topics
  1. C# Types
  2. C# boxing and unboxing
  3. C# DataTypes
  4. C# type conversions
More Related Topics.....
  1. C# Access Modifiers , CSharp Access Specifiers
  2. How to CultureInfo in C#
  3. How do I solve System.InvalidCastException?
  4. Difference between readonly and const keyword
  5. Difference Between Task and Thread
  6. Object reference not set to an instance of an object
  7. How to Convert Char to String in C#
  8. How to Convert Char Array to String in C#
  9. How to convert int to string in C#?
Related Links
  1. C# - Operating System Information
  2. Start and Kill Processes in C#
  3. Random Number Generator in C#

Từ khóa » Hh Mm Ss C#