Wednesday, 23 October 2013

C#.NET - Standard DateTime formats with examples

using System;

class Program
{
    static void Main(string[] args)
    {

        string str = string.Empty;

       //A standard date and time format string uses a single format specifier
      //to define the text representation of a date and time value.
      //The following examples describes the standard date and time format specifiers.

        //d Short date Pattern
        str = DateTime.Now.ToString("d");  
        //10/23/2013

        //d Long date Pattern
        str = DateTime.Now.ToString("D");  
        //Wednesday, October 23, 2013

        //f Full date time (short time) Pattern
        str = DateTime.Now.ToString("f");  
        //Wednesday, October 23, 2013 5:59 PM

        //F Full date time (long time) Pattern
        str = DateTime.Now.ToString("F");  
        //Wednesday, October 23, 2013 5:59:39 PM

        //g General date time (short time) Pattern
        str = DateTime.Now.ToString("g");  
        //10/23/2013 5:59 PM

        //G General date time (long time) Pattern
        str = DateTime.Now.ToString("G");  
        //10/23/2013 5:59:56 PM

        //m or M Month day Pattern
        str = DateTime.Now.ToString("m");  
        //October 23
        str = DateTime.Now.ToString("M");  
        //October 23

        //r or R RFC1123 Pattern
        str = DateTime.Now.ToString("r");  
        //Wed, 23 Oct 2013 18:01:02 GMT
        str = DateTime.Now.ToString("R");
        //Wed, 23 Oct 2013 18:01:02 GMT

        //s Sortable date time pattern
        str = DateTime.Now.ToString("s");  
        //2013-10-23T18:01:22

        //t Short time pattern
        str = DateTime.Now.ToString("t");  
        //6:01 PM

        //T Long time pattern.
        str = DateTime.Now.ToString("T");  
        //6:05:25 PM

        //u Universal sortable date time pattern
        str = DateTime.Now.ToString("u");  
        //2013-10-23 18:06:25Z

        //U Universal full date/time pattern
        str = DateTime.Now.ToString("U");  
        //Wednesday, October 23, 2013 12:36:50 PM  

        //y or Y Year month pattern.
        str = DateTime.Now.ToString("y");  
        //October, 2013
        str = DateTime.Now.ToString("Y");  
        //October, 2013

    }
}

No comments: