Open In App

C# Program to Display Date in String

Last Updated : 27 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

System namespace and mscorlib.dll assembly in C# language provide a variety of classes and structures. It also provides DateTime struct using which we can initialize Data and Time objects. Using this struct we can also determine the year, month, weekday, etc.

1. DateTime Constructor: DateTime constructor is used for initializing an instance of the DateTime struct. The constructor method accepts year, month, day, time as parameters. 

Syntax:

DateTime myDate = new DateTime(YYYY, MM, DD);    

Note that the DateTime struct consists of a total of eleven overloaded constructors defined for different purposes. This article only focuses upon how we can display Date in string format in C# so we would not discuss all the remaining constructors.

2. DateTime.now() method: To get the current date and time DateTime.now() method of the DateTime struct is used. It returns an object of the DateTime struct which can be presented as a string using the ToString() method as explained below.

Syntax:

DateTime.now(format);

Return type: Returns an object of DateTime struct that contains the current date and time

3. ToString() method: This is quite useful whenever we want to print an object in string format. This method is used to convert the current DateTime object value to a string using the specified format.

Syntax:

DateTime.ToString(format);

Return type: Returns a string that represents the current date and time

Displaying date as a string in C#

There are numerous formats to display a date in the console. For example, Date/Month/Year, Month/Date/Year, etc. C# provides a number of overloaded DateTime.now() constructor methods that help to print date in various formats. Some of the commonly used methods are discussed below in detail:

1. This method is used to create an object of DateTime struct that contains current data in the format “Month/Day/Year”. Since it returns an instance of the DateTime struct so we can use it as a string by using the ToString() method. For example, 12/04/2021.

Syntax:

DateTime.Now.ToString(“MM/dd/yyyy”)

Return type: Returns a string format of the current date

2. This method is used to create an object of DateTime struct that contains current data in the format “Month Date”. Since it returns an instance of the DateTime struct so we can use it as a string using the ToString() method. For example, December 04

Syntax:

DateTime.Now.ToString(“MMMM dd”)

Return type: Returns a string format of the current date

3. This method is used to create an object of the DateTime struct that contains current data in the format. Since it returns an instance of the DateTime struct so we can use it as a string using the ToString() method. For example, Saturday,03 December 2021 04:10 AM

Syntax:

DateTime.Now.ToString(“dddd, dd MMMM yyyy”) 

Return type: Returns a string format of the current date

4. This method is used to create an object of the DateTime struct that contains current data in the format. Since it returns an instance of the DateTime struct so we can use it as a string using the ToString() method. For example, Saturday, 04 December 2021 06:40:59

Syntax:

DateTime.Now.ToString(“dddd, dd MMMM yyyy HH:mm:ss”)

Return type: Returns a string format of the current date

5. This method is used to create an object of the DateTime struct that contains current data in the format. Since it returns an instance of the DateTime struct so we can use it as a string using the ToString() method. For example, 12/04/2021 06:41

Syntax:

DateTime.Now.ToString(“MM/dd/yyyy HH:mm”)

Return type: Returns a string format of the current date

6. This method is used to create an object of the DateTime struct that contains current data in the format. Since it returns an instance of the DateTime struct so we can use it as a string using the ToString() method. For example, 12/04/2021 06:44 AM

Syntax:

DateTime.Now.ToString(“MM/dd/yyyy hh:mm tt”)

Return type: Returns a string format of the current date

7. This method is used to create an object of the DateTime struct that contains current data in the format. Since it returns an instance of the DateTime struct so we can use it as a string using the ToString() method. For example, 12/04/2021 6:44

Syntax:

DateTime.Now.ToString(“MM/dd/yyyy H:mm”)

Return type: Returns a string format of the current date

8. This method is used to create an object of the DateTime struct that contains current data in the format. Since it returns an instance of the DateTime struct so we can use it as a string using the ToString() method. For example, 2021’-‘12’-‘04’T’07’:’33’:’15.9841280+00:00

Syntax:

DateTime.Now.ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK”)

Return type: Returns a string format of the current date

9. This method is used to create an object of the DateTime struct that contains current data in the format. Since it returns an instance of the DateTime struct so we can use it as a string using the ToString() method. For example, Sat, 04 Dec 2021 07’:’34’:’42 ‘G12T

Syntax:

DateTime.Now.ToString(“ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT”)

Return type: Returns a string format of the current date

10. This method is used to create an object of the DateTime struct that contains current data in the format. Since it returns an instance of the DateTime struct so we can use it as a string using the ToString() method. For example, 2021’-‘12’-‘04’T’07’:’41’:’12

Syntax:

DateTime.Now.ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss”)

Return type: Returns a string format of the current date

Below is the implementation to illustrate these methods.

Example:

C#




// C# program to illustrate how we can
// display date and time in a string format
using System;
 
class GFG{
 
static public void Main()
{
     
    // Getting the string form of the current date in
    // a format i.e, 12/04/2021  
    string currentDate1 = DateTime.Now.ToString("MM/dd/yyyy");
       
    // Displaying the current date
    Console.WriteLine("currentDate1 : " + currentDate1);
     
    // Getting the string form of the current date in
    // a format i.e, Saturday, 04 December 2021
    string currentDate2 = DateTime.Now.ToString("dddd, dd MMMM yyyy");
 
    // Displaying the current date
       Console.WriteLine("currentDate2 : " + currentDate2);
 
    // Getting the string form of the current date
    // in a format i.e, December 04                 
    string currentDate3 = DateTime.Now.ToString("MMMM dd");
 
    // Displaying current date     
    Console.WriteLine("currentDate3 : " + currentDate3);
 
    // Getting the string form of the current date
    // in a format, Saturday, i.e, 04 December 2021 19:12:29         
    string currentDate4 = DateTime.Now.ToString(
        "dddd, dd MMMM yyyy HH:mm:ss");
 
    // Displaying current date     
    Console.WriteLine("currentDate4 : " + currentDate4);
     
    // Getting the string form of the current date
    // in a format, i.e, 12/04/2021 19:12         
    string currentDate5 = DateTime.Now.ToString("MM/dd/yyyy HH:mm");
 
    // Displaying current date     
    Console.WriteLine("currentDate5 : " + currentDate5);
     
    // Getting the string form of the current date
    // in a format, i.e, 12/04/2021 07:12 PM         
    string currentDate6 = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");
 
    // Displaying current date     
    Console.WriteLine("currentDate6 : " + currentDate6);
     
    // Getting the string form of the current date in
    // a format,i.e, 12/04/2021 19:12        
    string currentDate7 = DateTime.Now.ToString("MM/dd/yyyy H:mm");
 
    // Displaying current date     
    Console.WriteLine("currentDate7 : " + currentDate7);
     
    // Getting the string form of the current date in
    // a format, 2021’-‘12’-‘04’T’19’:’12’:’29.6715830+00:00         
    string currentDate8 = DateTime.Now.ToString(
        "yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK");
 
    // Displaying current date
    Console.WriteLine("currentDate8 : " + currentDate8);
     
    // Getting the string form of the current date in
    // a format, Sat, 04 Dec 2021 19’:’12’:’29 ‘G12T         
    string currentDate9 = DateTime.Now.ToString(
        "ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT");
 
    // Displaying current date     
    Console.WriteLine("currentDate9 : " + currentDate9);
     
    // Getting the string form of the current date in
    // a format, 2021’-‘12’-‘04’T’19’:’12’:’29         
    string currentDate10 = DateTime.Now.ToString(
        "yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss");
 
    // Displaying current date     
    Console.WriteLine("currentDate10 : " + currentDate10);
}
}


Output:

currentDate1 : 12/07/2021
currentDate2 : Tuesday, 07 December 2021
currentDate3 : December 07
currentDate4 : Tuesday, 07 December 2021 09:00:37
currentDate5 : 12/07/2021 09:00
currentDate6 : 12/07/2021 09:00 AM
currentDate7 : 12/07/2021 9:00
currentDate8 : 2021’-‘12’-‘07’T’09’:’00’:’37.9475430+05:30
currentDate9 : Tue, 07 Dec 2021 09’:’00’:’37 ‘G12T
currentDate10 : 2021’-‘12’-‘07’T’09’:’00’:’37


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads