C# Print date and time in different formats

C# DateTime Class Example: Here, we will figure out how to print the current date and time in various date and times arranges in C#?

Following organizations are utilized to print date and time in various arrangements,

Format Description
dThis is utilized to speak to the day of any month, its value lies between 1 to 31.
ddThis is likewise used to speak to the day of any month, its value lies between 1 to 31.
ddd This is utilized to speak to the name of the day in three-character like (Mon, Tue, and so on).
dddd This is utilized to speak to the complete name of the day like (Monday, Tuesday, and so on).
h This is utilized for a 12-hour clock for example 5.
hh This is utilized for 12-hour with left padded 0 for example 05.
H This is utilized for a 24-hour clock for example 14.
HH This is utilized for 24-hour with left padded 0 for example 05.
m This is utilized to speak to minutes.
mm This is utilized to speak to minutes with left padded 0 for example 07.
M This is utilized to speak to the month number.
MM This is utilized to speak to month number with left padded 0.
MMM This is utilized to speak to month name in 3 characters like (Jan, Feb, and so on).
MMMM This is utilized to speak to an entire month name like (January).
s This is utilized to speak to second.
ss This is utilized to speak to second with left padded 0.
t This is utilized to speak to AM or PM for example(A or P).
tt This is utilized to speak to AM or PM for example(AM or PM).
y This is utilized to speak to the year.
yy This is utilized to speak to year with left padded 0.
yyy This is utilized to speak to an entire year number like 2019.
yyyy This is utilized to speak to an entire year number like 2019.

Underneath program shows how we can print the present date and time in various date time designs in C#?

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get current date and time using .Now property
            DateTime dt = DateTime.Now;

            //Now we print all possible date and time formates.
            Console.WriteLine(dt.ToString("yyyy MMMM"));
            Console.WriteLine(dt.ToString("HH:mm:ss"));
            Console.WriteLine(dt.ToString("h:mm tt"));
            Console.WriteLine(dt.ToString("H:mm"));
            Console.WriteLine(dt.ToString("hh:mm tt"));
            Console.WriteLine(dt.ToString("HH:mm"));
            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'-'MM'-'dd'T'HH':'mm':'ss.fffffffK"));
            Console.WriteLine(dt.ToString("MMMM dd"));
            Console.WriteLine(dt.ToString("MM/dd/yyyy HH:mm:ss"));
            Console.WriteLine(dt.ToString("MM/dd/yyyy h:mm tt"));
            Console.WriteLine(dt.ToString("MM/dd/yyyy H:mm"));
            Console.WriteLine(dt.ToString("MM/dd/yyyy HH:mm"));
            Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
            Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy"));
            Console.WriteLine(dt.ToString("MM/dd/yyyy"));
            Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy")); 
            Console.WriteLine(dt.ToString("MM/dd/yyyy hh:mm tt"));
            
            Console.WriteLine();  
        }
    }
}

Output

2019 November
08:06:01
8:06 AM
8:06
08:06 AM
08:06
Sat, 02 Nov 2019 08:06:01 GMT
2019-11-02T08:06:01
2019-11-02T08:06:01.0862340+00:00
November 02
11/02/2019 08:06:01
11/02/2019 8:06 AM
11/02/2019 8:06
11/02/2019 08:06
Saturday, 02 November 2019 08:06:01
Saturday, 02 November 2019
11/02/2019
Saturday, 02 November 2019
11/02/2019 08:06 AM

Leave a Comment

error: Alert: Content is protected!!