The current date is 10/06/2021. In the United States, this would be formatted as 10/06/2021. However, in some other cultures, this would be formatting as 06/10/2021.
In C# programming language, there are two different methods for specifying a specific format of a date. The first is by using the overload method. C# allows you to change specific parts of an object by using the overloaded methods provided. Using this format, all C# has to do is specify the date part that needs to be changed.
For example, if C# wanted to have a date string formatted as “Month dd yyyy” then C# would use this code:
DateTime.Now.ToString("MMM dd, yyyy")
The second method C# uses for formatting a specific date format is by calling a specific method. This method can be used within C# but C# isn’t required to do so. C# might use this method if C# wanted to have a date formatted as “MMM d, yyyy” instead of the first example’s “Month dd yyyy”. C# would use this code:
Below is a fully working example.
DateTime audate = DateTime.Today; Console.WriteLine(audate.ToString("F", new System.Globalization.CultureInfo("en-au"))); Console.WriteLine(audate.ToString("dd/MM/yyyy"));
The output from the above code is. For convenience, I have used two formats.
Wednesday, 6 October 2021 12:00:00 am 06/10/2021