C# | Print date and time in different formats

C# DateTime Class Example: Here, we are going to learn how to print current date and time in different date and times formats in C#? Submitted by IncludeHelp, on November 02, 2019

Different formats to print date and time in different formats

Following formats are used to print date and time in different formats,

FormatDescription
d This is used to represent the day of any month, its value lies between 1 to 31.
dd This is also used to represent the day of any month, its value lies between 1 to 31.
ddd This is used to represent the name of the day in three-character like (Mon, Tue, etc).
dddd This is used to represent the full name of the day like (Monday, Tuesday, etc).
h This is used for a 12-hour clock for example 5.
hh This is used for 12-hour with left padded 0 for example 05.
H This is used for a 24-hour clock for example 14.
HH This is used for 24-hour with left padded 0 for example 05.
m This is used to represent minutes.
mm This is used to represent minutes with left padded 0 for example 07.
M This is used to represent the month number.
MM This is used to represent month number with left padded 0.
MMM This is used to represent month name in 3 characters like (Jan, Feb, etc).
MMMM This is used to represent a full month name like (January).
s This is used to represent second.
ss This is used to represent second with left padded 0.
t This is used to represent AM or PM for example(A or P).
tt This is used to represent AM or PM for example(AM or PM).
y This is used to represent the year.
yy This is used to represent year with left padded 0.
yyy This is used to represent a full-year number like 2019.
yyyy This is used to represent a full-year number like 2019.

C# program to print the current date and time in different date time formats

Below program shows how we can print the current date and time in different date time formats 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

All C#.Net Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.