C# Program to Display Date in String
Last Updated :
27 Dec, 2021
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
Similar Reads
Introduction
C# TutorialC# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET FrameworkThe .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# .NET Framework (Basic Architecture and Component Stack)C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Hello WorldThe Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following:A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Main
4 min read
Common Language Runtime (CLR) in C#The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages the execution of .NET applications. It is responsible for loading and executing the code written in various .NET programming languages, including C#, VB.NET, F#, and others.When a C# program is compiled, th
4 min read
Fundamentals
C# IdentifiersIn programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
C# Data TypesData types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# VariablesIn C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
4 min read
C# LiteralsIn C#, a literal is a fixed value used in a program. These values are directly written into the code and can be used by variables. A literal can be an integer, floating-point number, string, character, boolean, or even null. Example:// Here 100 is a constant/literal.int x = 100; Types of Literals in
5 min read
C# OperatorsIn C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.Types of Operat
7 min read
C# KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error.Example:C#// C# Program to illustrate the
5 min read
Control Statements
C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions. These
5 min read
C# Switch StatementIn C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.
4 min read
C# LoopsLooping in a programming language is a way to execute a statement or a set of statements multiple times, depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.Types of Loops in C#Loops are mainly divided
4 min read
C# Jump Statements (Break, Continue, Goto, Return and Throw)In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#.Types of Jump StatementsThere are mainly five keywords in th
4 min read
OOP Concepts
Methods
Arrays
C# ArraysAn array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read
C# Jagged ArraysA jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows
4 min read
C# Array ClassArray class in C# is part of the System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the System.Collections namespace, but it is still considered as a collection because it is based on the IList interface. The Array class is the base clas
7 min read
How to Sort an Array in C# | Array.Sort() Method Set - 1Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows:Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Method
8 min read
How to find the rank of an array in C#Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on. Syntax: public int Rank { get; } Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. B
2 min read
ArrayList
String
Tuple
Indexers