Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
datetime

The properties of the DateTime class in C#

Aaron Xie

The DateTime class in C# offers several methods and properties to format various date formats. Below you will see all the patterns of the DateTime class and its results.

DateTime.Now.ToString("MM/dd/yyyy") - 05/08/2020

DateTime.Now.ToString("dddd, dd MMMM yyyy") - Friday, 08 May 2020

DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss") - Friday, 08 May 2020 11:30:06

DateTime.Now.ToString("MM/dd/yyyy HH:mm") - 05/08/2020 11:30

DateTime.Now.ToString("MM/dd/yyyy hh:mm tt") - 05/08/2020 11:30 AM

DateTime.Now.ToString("MM/dd/yyyy H:mm") - 05/08/2020 11:30

DateTime.Now.ToString("MM/dd/yyyy h:mm tt") - 05/08/2020 11:30 AM

DateTime.Now.ToString("MMMM dd") - May 08

DateTime.Now.ToString("HH:mm") - 11:30

DateTime.Now.ToString("hh:mm tt") - 11:30 AM

DateTime.Now.ToString("H:mm") - 11:30

DateTime.Now.ToString("h:mm tt") - 11:30 AM

DateTime.Now.ToString("HH:mm:ss") - 11:30:06

DateTime.Now.ToString("yyyy MMMM") - 2020 May

Abbreviations

  • d represents the day of the month as a number from 1 through 31.

  • dd represents the day of the month as a number from 01 through 31.

  • ddd represents the abbreviated name of the day (Mon, Tues, etc).

  • dddd represents the full name of the day (Monday, Tuesday, Wednesday, etc).

  • h represents the 12-hour clock hour

  • hh represents the 12-hour clock, with a leading 0

  • H represents the 24-hour clock hour

  • HH represents the 24-hour clock hour, with a leading 0

  • m represents minutes

  • mm represents minutes with a leading zero

  • M represents the month number

  • MM represents the month number with leading zero

  • MMM represents the abbreviated month Name

  • MMMM represents the full month name

  • s represents seconds

  • ss represents seconds with leading zero

  • y represents the year, no leading zero (2020 would be 20)

  • yy represents the year, leading zero (2020 would be 020)

  • yyy represents the year (2020)

If you want to read more about DateTime in C#, check out its documentation.

RELATED TAGS

c#
datetime

CONTRIBUTOR

Aaron Xie
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring