C# Program to Display the Date in Various Formats

This is a C# Program to display the date in various formats.

Problem Description

This C# Program Displays the Date in Various Formats.

Problem Solution

Here the Date is Displayed in various Formats.

Program/Source Code

Here is source code of the C# Program to Display the Date in Various Formats . The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Display the Date in Various Formats 
 */
using System;
namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime date = new DateTime(2013,6, 23);
            Console.WriteLine("Some 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"));
            Console.Read();
            return 0;
        }
    }
}
Program Explanation

In this C# Program, we are creating the date object to the DateTime() constructor to get any date. The Date Format Specifier is used to represent a date in various ways. Representation of a date can be done by one of two Date Format Specifiers that is a Short Date Format Specifier or a Long Date Format Specifier.

advertisement
advertisement
Runtime Test Cases
 
Some Date Formats :
Date and Time : 6/23/2013 12:00:00 AM
2013-06-23
23-Jun-13
6/23/2013
6/23/13
06/23/2013
06/23/13
13/06/23

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.