In this tutorial, we will write a C# program to display date formats, you see the list of various ways in which you can display dates in C#.
A new date object is created for DateTime class in C#. DateTime class is already defined class present in C# language. We do not have to create such a class ourselves. Then simple it displays the date in various formats.
C# Program to display various Date Formats
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; class Program { static void Main() { DateTime date = new DateTime(2013, 6, 23); Console.WriteLine("Some of the Date Formats : "); Console.WriteLine("Date and Time: {0}", date); Console.WriteLine(date.ToString("yyyy-MM-dd")); Console.WriteLine(date.ToString("dd-MMM-yy")); Console.WriteLine(date.ToString("M/d/yyyy")); Console.WriteLine(date.ToString("M/d/yy")); Console.WriteLine(date.ToString("MM/dd/yyyy")); Console.WriteLine(date.ToString("MM/dd/yy")); Console.WriteLine(date.ToString("yy/MM/dd")); } } |
Output: