How to use DateTime in C#
DateTime
DateTime is a built-in class in C# used to store dates and parse them to and from strings.
Usage
There are various ways to format time in C#:
using System;class HelloWorld{static void Main(){// Storing date as Stringstring dateString = "15/07/2020 06:30:45 PM";// Directly initializing date as DateTime variableDateTime date1 = new DateTime(2020, 7, 15, 18, 30, 45);// Parsing string to DateTimeDateTime date2 = DateTime.ParseExact(dateString, "dd/MM/yyyy hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);// Both dates output 07 15 20 18:30:45Console.WriteLine(date1.ToString("MM dd yy HH:mm:ss"));Console.WriteLine(date2.ToString("MM dd yy HH:mm:ss"));// Outputs Wednesday July, 2020 06:30:45 PMConsole.WriteLine(date1.ToString("dddd MMMM, yyyy hh:mm:ss tt"));// Outputs 07/15/2020Console.WriteLine(date1.ToString("MM/dd/yyyy"));// Outputs 06:30:45 PMConsole.WriteLine(date1.ToString("hh:mm:ss tt"));}}
This example demonstrates how DateTime variables are initialized and then parsed to strings. Parsing to and from a string can be done by comparing the string to a user-defined format.
C# allows users to define their own format using strings. Different components of time are represented in the format as different strings. Some of these are:
- d represents the day without a leading . It ranges from to .
- dd represents the day with a leading . It ranges from to .
- ddd represents an abbreviated name for a day like Mon or Tues.
- dddd represents the full name for a day like Monday or Tuesday.
- M is the month number without a leading zero. It ranges from to .
- MM is the month number with a leading zero. It ranges from to .
- MMM is an abbreviated name of a month like Jan or Feb.
- MMMM is the full name of a month like January or February.
- y is the year with a minimum of one digit. For instance, 2001 would be .
- yy is the year with a minimum of two digits. For instance, 2001 would be .
- yyy is the year with a minimum of three digits. For instance, 2001 would be .
- yyyy is a four-digit representation of a year. For instance, 2001 would be .
- h is the hour in 12-hour notation without a leading zero. For example, 4 AM and 4 PM would both be .
- hh is the hour in 12-hour notation with a leading zero. For example, 4 AM and 4 PM would both be .
- H is the hour in 24-hour notation without a leading zero. For example, 4 AM and 4 PM would be and , respectively.
- HH is the hour in 24-hour notation with a leading zero. For example, 4 AM and 4 PM would be and , respectively.
- m is the minutes without a leading zero. It ranges from to .
- mm is the minutes with a leading zero. It ranges from to .
- s is the seconds without a leading zero. It ranges from to .
- ss is the seconds with a leading zero. It ranges from to .
- t is a single-character abbreviation for AM and PM. AM will become A and PM will become P.
- tt is AM and PM.
- K is the time-zone. For example, .
- z represents a minimum single-digit offset from the Coordinated Universal Timezone in hours. For example, .
- zz represents a minimum two-digit offset from the Coordinated Universal Timezone in hours. For example, .
- zz represents an offset from the Coordinated Universal Timezone in hours and minutes. For example, .
- f represents the decimals in a seconds value. The number of decimal digits is equal to the number of f’s in the format – there can be up to seven f’s. For example, fff may show for a seconds value of 2:22459 and for 2:00000.
- F represents the non-zero decimals in a seconds value. The number of decimal digits is equal to the number of F’s in the format – there can be up to seven F’s. For example, FFF may show for a seconds value of 2:22459, but it would show nothing for 2:00000.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved