C# DateTime Format

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
C# DateTime.ToString Method

Ticks in DateTime

The Ticks property expresses date and time values in units of one ten-millionth of a second. The Millisecond property returns the thousandths of a second in a date and time value. There are 10,000 ticks in a millisecond, or 10 million ticks in a second. From the following C# example, you can get the milliseconds since 1/1/2000 using such code:

using System; namespace DateTimeTicks { class Program { private static DateTime fromDate = new DateTime(2000, 1, 1); static void Main(string[] args) { Console.WriteLine((long)((DateTime.Now.ToUniversalTime() - fromDate).TotalMilliseconds + 0.5)); Console.ReadKey(); } } }

DateTime for a specific culture

CultureInfo provides information about a specific culture . The information includes the names for the culture, the writing system, the calendar used, the sort order of strings, and formatting for dates and numbers.

using System.Globalization;

When you format a DateTime with DateTime.ToString() you can also specify the culture to use.

using System; using System.Globalization; namespace CustomDateTime { class Program { static void Main(string[] args) { DateTime dt = DateTime.Now; // French CultureInfo CultureInfo frC = new CultureInfo("fr-FR"); Console.WriteLine(dt.ToString("f", frC)); Console.WriteLine(dt.ToString("d", frC)); Console.WriteLine(dt.ToString("t", frC)); // German CultureInfo CultureInfo deC = new CultureInfo("de-De"); Console.WriteLine(dt.ToString("f", deC)); Console.WriteLine(dt.ToString("d", deC)); Console.WriteLine(dt.ToString("t", deC)); // Spanish CultureInfo CultureInfo esC = new CultureInfo("es-ES"); Console.WriteLine(dt.ToString("f", esC)); Console.WriteLine(dt.ToString("d", esC)); Console.WriteLine(dt.ToString("t", esC)); Console.ReadKey(); } } }
output
c# datetime format yyyy-mm-dd

Convert DateTime to String

You can convert a DateTime value to a formatted string like this:

date.ToString("yyyyMMdd");

You can convert your string to a DateTime value like this:

DateTime date = DateTime.Parse(something);

Convert String to DateTime

You have basically two options for this.

  1. DateTime.Parse()
  2. DateTime.ParseExact()

You can parse user input like this:

DateTime dt = DateTime.Parse(dStr);

If you have a specific format for the string, you should use the other method:

DateTime dt = DateTime.ParseExact(dStr, "d", null);

The "d" stands for the short date pattern and null specifies that the current culture should be used for parsing the string .