DateTime Format C#

Date and Time in C# are handled by DateTime class in C# that provides properties and methods to format dates in different datetime formats. This article blog explains how to work with date and time format in C#. 

The following table describes various C# DateTime formats and their results. Here we see all the patterns of the C# DateTime, format, and results.


DateTime.Now.ToString("MM/dd/yyyy")             //Res: 05/29/2015
DateTime.Now.ToString("dddd, dd MMMM yyyy")	//Res: Friday, 29 May 2015
DateTime.Now.ToString("dddd, dd MMMM yyyy")	//Res: Friday, 29 May 2015 05:50
DateTime.Now.ToString("dddd, dd MMMM yyyy")	//Res: Friday, 29 May 2015 05:50 AM
DateTime.Now.ToString("dddd, dd MMMM yyyy")	//Res: Friday, 29 May 2015 5:50
DateTime.Now.ToString("dddd, dd MMMM yyyy")	//Res: Friday, 29 May 2015 5:50 AM
DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")	//Res: Friday, 29 May 2015 05:50:06
DateTime.Now.ToString("MM/dd/yyyy HH:mm")	//Res: 05/29/2015 05:50
DateTime.Now.ToString("MM/dd/yyyy hh:mm tt")	//Res: 05/29/2015 05:50 AM
DateTime.Now.ToString("MM/dd/yyyy H:mm")	//Res: 05/29/2015 5:50
DateTime.Now.ToString("MM/dd/yyyy h:mm tt")	//Res: 05/29/2015 5:50 AM
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")	//Res: 05/29/2015 05:50:06
DateTime.Now.ToString("MMMM dd")	        //Res: May 29
DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK")	          //Res: 2015-05-16T05:50:06.7199222-04:00
DateTime.Now.ToString("ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’")	          //Res: Fri, 16 May 2015 05:50:06 GMT
DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss")	                  //Res: 2015-05-16T05:50:06
DateTime.Now.ToString("HH:mm")	        //Res: 05:50
DateTime.Now.ToString("hh:mm tt")	//Res: 05:50 AM
DateTime.Now.ToString("H:mm")	        //Res: 5:50
DateTime.Now.ToString("h:mm tt")	//Res: 5:50 AM
DateTime.Now.ToString("HH:mm:ss")	//Res: 05:50:06
DateTime.Now.ToString("yyyy MMMM")	//Res: 2015 May

 

Details:

  1. d -> Represents the day of the month as a number from 1 through 31.

  2. dd -> Represents the day of the month as a number from 01 through 31.

  3. ddd-> Represents the abbreviated name of the day (Mon, Tues, Wed, etc).

  4. dddd-> Represents the full name of the day (Monday, Tuesday, etc).

  5. h-> 12-hour clock hour (e.g. 4).

  6. hh-> 12-hour clock, with a leading 0 (e.g. 06)

  7. H-> 24-hour clock hour (e.g. 15)

  8. HH-> 24-hour clock hour, with a leading 0 (e.g. 22)

  9. m-> Minutes

  10. mm-> Minutes with a leading zero

  11. M-> Month number(eg.3)

  12. MM-> Month number with leading zero(eg.04)

  13. MMM-> Abbreviated Month Name (e.g. Dec)

  14. MMMM-> Full month name (e.g. December)

  15. s-> Seconds

  16. ss-> Seconds with leading zero

  17. t-> Abbreviated AM / PM (e.g. A or P)

  18. tt-> AM / PM (e.g. AM or PM

  19. y-> Year, no leading zero (e.g. 2015 would be 15)

  20. yy-> Year, leading zero (e.g. 2015 would be 015)

  21. yyy-> Year, (e.g. 2015)

  22. yyyy-> Year, (e.g. 2015)

  23. K-> Represents the time zone information of a date and time value (e.g. +05:00)

  24. z-> With DateTime values represents the signed offset of the local operating system's time zone from

    Coordinated Universal Time (UTC), measured in hours. (e.g. +6)

  25. zz-> As z, but with leading zero (e.g. +06)

  26. zzz-> With DateTime values represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. (e.g. +06:00)

  27. f-> Represents the most significant digit of the seconds' fraction; that is, it represents the tenths of a second in a date and time value.

  28. ff-> Represents the two most significant digits of the seconds' fraction in date and time

  29. fff-> Represents the three most significant digits of the seconds' fraction; that is, it represents the milliseconds in a date and time value.

  30. ffff-> Represents the four most significant digits of the seconds' fraction; that is, it represents the ten-thousandths of a second in a date and time value. While it is possible to display the ten-thousandths of a second component of a time value, that value may not be meaningful.

  31. fffff-> Represents the five most significant digits of the seconds' fraction; that is, it represents the hundred-thousandths of a second in a date and time value.

  32. ffffff-> Represents the six most significant digits of the seconds' fraction; that is, it represents the millionths of a second in a date and time value.

  33. fffffff-> Represents the seven most significant digits of the second's fraction; that is, it represents the ten-millionths of a second in a date and time value.

 

Here is a complete C# code sample that uses these formats. 


using System;  
  
namespace FormatDateTime
{  
    public class DateTimeFormatter  
    {  
        public static string DateTimeFormatter(int type, DateTime dateTime)
        {
            var result = string.Empty;
            switch (type)
            {
                case 1: result = dateTime.ToString("MM/dd/yyyy"); break;
                case 2: result = dateTime.ToString("dddd, dd MMMM yyyy"); break;
                case 3: result = dateTime.ToString("dddd, dd MMMM yyyy"); break;
                case 4: result = dateTime.ToString("dddd, dd MMMM yyyy"); break;
                case 5: result = dateTime.ToString("dddd, dd MMMM yyyy"); break;
                case 6: result = dateTime.ToString("dddd, dd MMMM yyyy"); break;
                case 7: result = dateTime.ToString("dddd, dd MMMM yyyy HH:mm:ss"); break;
                case 8: result = dateTime.ToString("MM/dd/yyyy HH:mm"); break;
                case 9: result = dateTime.ToString("MM/dd/yyyy hh:mm tt"); break;
                case 10: result = dateTime.ToString("MM/dd/yyyy H:mm"); break;
                case 11: result = dateTime.ToString("MM/dd/yyyy h:mm tt"); break;
                case 12: result = dateTime.ToString("MM/dd/yyyy HH:mm:ss"); break;
                case 13: result = dateTime.ToString("MMMM dd"); break;
                case 14: result = dateTime.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK"); break;
                case 15: result = dateTime.ToString("ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’"); break;
                case 16: result = dateTime.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss"); break;
                case 17: result = dateTime.ToString("HH:mm"); break;
                case 18: result = dateTime.ToString("hh:mm tt"); break;
                case 19: result = dateTime.ToString("H:mm"); break;
                case 20: result = dateTime.ToString("h:mm tt"); break;
                case 21: result = dateTime.ToString("HH:mm:ss"); break;
                case 22: result = dateTime.ToString("yyyy MMMM"); break;
                default:
                    break;
            }
return result; } } }

 

Result For: Type = 2, DateTime = "Jan 01, 2021"


Friday, 01 January 2021

Summary: