DateTime format in c# - Change DateTime format in C#, Change date format in C#, dateTime format java, c# datetime format yyyy-mm-dd, C# TimeSpan format, Valid DateTime format C#, C# DateTime format milliseconds, DateTime C#

DateTime format in c#

DateTime format in c#: In this post, how to use DateTime format in C#. We use a formatting string with DateTime and ToString. Here we combine many format codes. Also: We need to specify a format string when using DateTime.ParseExact and DateTime.ParseExact.
Modified format. Here we modify the format string to get different outputs. We change some of the fields, the resulting string value is shorter.

C# program that uses a different format

using System;

class Program
{
    static void Main()
    {
        DateTime time = DateTime.Now;
        string dtformat = "M d h:mm yy";
        Console.WriteLine(time.ToString(dtformat));
    }
}

Output

09 13 11:48 20

Format string pattern

M       display one-digit month number
d       display one-digit day of the month
h       display one-digit hour on 12-hour
mm      display two-digit minutes
yy      display two-digit year

C# program that uses DateTime format

using System;

class Program
{
    static void Main()
    {
        // Use current time with a format string.
        DateTime time = DateTime.Now;
        string dtformat = "MMM ddd d HH:mm yyyy";
        Console.WriteLine(time.ToString(dtformat));
    }
}

Output

Feb Tue 11 13:15 2020

Format string pattern

MMM     display three-letter month
ddd     display three-letter day of the week
d       display day of the month
HH      display two-digit hours on 24-hour
mm      display two-digit minutes
yyyy    display four-digit year

These are standard formats. They are useful in many programs. Single char  DateTime format. We use a char with ToString or DateTime.ParseExact to specify a preset format.
DateTime.Parse

using System;

class Program
{
    static void Main()
    {        
        Console.WriteLine(dt.ToString("d"));
        Console.WriteLine(dt.ToString("D"));
        Console.WriteLine(dt.ToString("f"));
        Console.WriteLine(dt.ToString("F"));
        Console.WriteLine(dt.ToString("g"));
        Console.WriteLine(dt.ToString("G"));
        Console.WriteLine(dt.ToString("m"));
        Console.WriteLine(dt.ToString("M"));
        Console.WriteLine(dt.ToString("o"));
        Console.WriteLine(dt.ToString("O"));
        Console.WriteLine(dt.ToString("s"));
        Console.WriteLine(dt.ToString("t"));
        Console.WriteLine(dt.ToString("T"));
        Console.WriteLine(dt.ToString("u"));
        Console.WriteLine(dt.ToString("U"));
        Console.WriteLine(dt.ToString("y"));
        Console.WriteLine(dt.ToString("Y"));
		DateTime dt = DateTime.Now;
    }
}

Output


D    Friday, September 11, 2020
f    Friday, September 11, 2020 12:11 PM
F    Friday, September 11, 2020 12:12:22 PM
g    9/11/2020 12:12 PM
G    9/11/2020 12:12:22 PM
m    September 11
M    September 11
o    2020-09-11T12:12:22.1020000-08:00
O    2020-09-11T12:12:22.1020000-08:00
s    2020-09-11T12:12:22
t    12:12 PM
T    12:12:22 PM
u    2009-02-27 12:12:22Z
U    Friday, September 11, 2020 8:12:22 PM
y    September, 2020
Y    September, 2020
d    9/13/2020

Date strings: Here we see the ToLongTimeString, ToLongDateString, ToShortTimeString and ToShortDateString, methods on DateTime.

string.Format
C# program that uses ToString methods

using System;

class Program
{
    static void Main()
    {
        DateTime now = DateTime.Now;
        // Equivalent to D.
        Console.WriteLine(now.ToLongDateString());
        // Equivalent to T.
        Console.WriteLine(now.ToLongTimeString());
        // Equivalent to d.
        Console.WriteLine(now.ToShortDateString());
        // Equivalent to t.
        Console.WriteLine(now.ToShortTimeString());
        Console.WriteLine(now.ToString());
    }
}

Output

ToLongDateString     Friday, September 11, 2020
ToLongTimeString     12:20:59 PM
ToShortDateString    9/11/2020
ToShortTimeString    12:16 PM
ToString             9/11/2020 12:20:59 PM
Minutes format: Two lowercase ms has a leading zero if the number is only one digit long. We use the lowercase code "mm" for minutes. For minutes, we must use 2 chars.
Note: Just one "m" does not return minutes—it returns the month and day.

C# program that shows minute part

using System;

class Program
{
    static void Main()
    {
        // Create DateTime with 5 minutes.
        DateTime nineMinutes = new DateTime(2020, 4, 5, 3, 5, 0);
        Console.WriteLine("mm: {0}", nineMinutes.ToString("mm"));
    }
}

Output

mm: 05


Lowercase s: The lowercase s displays seconds. With ss we always want two digits, such as 00-59.

C# program that uses s format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
        DateTime dt = DateTime.Now;
        // Use space after s to avoid one-char date format.
        string result = now.ToString("s ");
        WriteLine($"{dt} [s] = {result}");
    }
}

Output

11/9/2020 12:04:17 PM [s] = 20

Hours format:

C# program that uses hours format

using System;

class Program
{
    static void Main()
    {
        // Create DateTime with 13 hours, or 1 PM.
        var result = new DateTime(2020, 11, 9, 13, 0, 0);
        Console.WriteLine("HH: {0}", result.ToString("HH"));
    }
}

Output

HH: 13

Day format: For days we use one to four d chars. One "d" and "dd" indicate the day of the month, while "ddd" and "dddd" indicate the day of the week, in a word.

C# program that uses "ddd" and "dddd"

using System;

class Program
{
    static void Main()
    {
        DateTime time = new DateTime(2020, 9, 11);
        // Console.WriteLine and string.Format can handle dates.
        Console.WriteLine("Two letters: {0:ddd}", time);
        Console.WriteLine("Three letters: {0:dddd}", time);
    }
}

Output

Two letters: Fri
Three letters: Friday
Complete day:
C# program that shows day strings

using System;

class Program
{
    static void Main()
    {
        DateTime dt = DateTime.Today;
        for (int i = 0; i < 7; i++)
        {
            Console.WriteLine(dt.ToString("dddd"));
            dt = dt.AddDays(1);
        }
    }
}

Output


Friday
Saturday
Sunday
Monday
Tuesday
Wednesday
Thursday

Three-letter days:

using System;

class Program
{
    static void Main()
    {
        DateTime dt = DateTime.Today;
        for (int i = 0; i < 7; i++)
        {
            Console.WriteLine(dt.ToString("ddd"));
            dt = dt.AddDays(1);
        }
    }
}

Output

Fri
Sat
Sun
Mon
Tue
Wed
Thu

Month format:

C# program that uses month formats

using System;

class Program
{
    static void Main()
    {        
        DateTime test = new DateTime(2020, 9, 9);
        Console.WriteLine("M:    {0}", test.ToString("M"));
        Console.WriteLine("MM:   {0}", test.ToString("MM"));
        Console.WriteLine("MMM:  {0}", test.ToString("MMM"));
        Console.WriteLine("MMMM: {0}", test.ToString("MMMM"));
    }
}

Output

M:    September 9
MM:   09
MMM:  Sep
MMMM: September
Era.
C# program that prints the current era

using System;

class Program
{
    static void Main()
    {
        // We are in A.D. era.
        Console.WriteLine(DateTime.Now.ToString("gg"));
    }
}

Output

A.D.

AM, PM Format: When you specify one t, you can get the first letter of the AM or PM string. This is equivalent to using Substring or getting the first char of the tt string.


C# program that displays AM and PM

using System;

class Program
{
    static void Main()
    {
        DateTime dt = DateTime.Now;
        for (int i = 0; i < 2; i++)
        {
            Console.WriteLine(dt.ToString("tt "));
            dt = dt.AddHours(12);
        }
    }
}

Output

PM
AM

Year Format:

Note: In almost all programs, we will not need three or five digits for the year, but these codes exist.

C# program that displays years

using System;

class Program
{
    static void Main()
    {
        DateTime now = DateTime.Now;
        Console.WriteLine(now.ToString("y "));
        Console.WriteLine(now.ToString("yy"));
        Console.WriteLine(now.ToString("yyy"));
        Console.WriteLine(now.ToString("yyyy"));
        Console.WriteLine(now.ToString("yyyyy"));
    }
}

Output

0
20
2020
2020
02020

FormatException: 

using System;

class Program
{
    static void Main()
    {
        const string dt = "*";
        string result = DateTime.Today.ToString(dt);
    }
}

Output

Unhandled Exception: System.FormatException: The input string was not in a correct format.
   at System.DateTimeFormat

.GetRealFormat
Null. When we format a DateTime with a format string that is null, empty, or missing, we get a default format. We can just omit the argument if we do not have a special format we need.
C# program that uses null, empty, missing formats

using System;

class Program
{
    static void Main()
    {
        var dt = new DateTime(2000, 9, 11);
        Console.WriteLine("NULL FORMAT STRING:    {0}", dt.ToString((string)null));
  Console.WriteLine("MISSING FORMAT STRING: {0}", dt.ToString());
        Console.WriteLine("EMPTY FORMAT STRING:   {0}", dt.ToString(""));        
    }
}

Output

NULL FORMAT STRING:    9/11/2020 12:00:00 AM
MISSING FORMAT STRING: 9/11/2020 12:00:00 AM
EMPTY FORMAT STRING:   9/11/2020 12:00:00 AM

Tags:-
Change DateTime format in C#, Change date format in C#, dateTime format java, c# datetime format yyyy-mm-dd, C# TimeSpan format, Valid DateTime format C#, C# DateTime format milliseconds, DateTime C#

2021-10-03