Use the ToString() method to convert a DateTime to a string : DateTime Format : Date Time C# Examples


C# Examples » Date Time » DateTime Format »

 

Use the ToString() method to convert a DateTime to a string









    
using  System;

class  MainClass
{
    public  static  void  Main()
    {
        DateTime  myDateTime  =  new  DateTime(2004,  1,  12,  22,  2,  10);
        Console.WriteLine("myDateTime.ToString()  =  "  +  myDateTime.ToString());
        Console.WriteLine("myDateTime.ToString(\"d\")  =  "  +  myDateTime.ToString("d"));
        Console.WriteLine("myDateTime.ToString(\"D\")  =  "  +  myDateTime.ToString("D"));
        Console.WriteLine("myDateTime.ToString(\"f\")  =  "  +  myDateTime.ToString("f"));
        Console.WriteLine("myDateTime.ToString(\"F\")  =  "  +  myDateTime.ToString("F"));
        Console.WriteLine("myDateTime.ToString(\"g\")  =  "  +  myDateTime.ToString("g"));
        Console.WriteLine("myDateTime.ToString(\"G\")  =  "  +  myDateTime.ToString("G"));
        Console.WriteLine("myDateTime.ToString(\"m\")  =  "  +  myDateTime.ToString("m"));
        Console.WriteLine("myDateTime.ToString(\"r\")  =  "  +  myDateTime.ToString("r"));
        Console.WriteLine("myDateTime.ToString(\"s\")  =  "  +  myDateTime.ToString("s"));
        Console.WriteLine("myDateTime.ToString(\"t\")  =  "  +  myDateTime.ToString("t"));
        Console.WriteLine("myDateTime.ToString(\"T\")  =  "  +  myDateTime.ToString("T"));
        Console.WriteLine("myDateTime.ToString(\"u\")  =  "  +  myDateTime.ToString("u"));
        Console.WriteLine("myDateTime.ToString(\"U\")  =  "  +  myDateTime.ToString("U"));
        Console.WriteLine("myDateTime.ToString(\"y\")  =  "  +  myDateTime.ToString("y"));

    }
}
    
   
  
   



Output

myDateTime.ToString() = 12/01/2004 10:02:10 PM
myDateTime.ToString("d") = 12/01/2004
myDateTime.ToString("D") = January 12, 2004
myDateTime.ToString("f") = January 12, 2004 10:02 PM
myDateTime.ToString("F") = January 12, 2004 10:02:10 PM
myDateTime.ToString("g") = 12/01/2004 10:02 PM
myDateTime.ToString("G") = 12/01/2004 10:02:10 PM
myDateTime.ToString("m") = January 12
myDateTime.ToString("r") = Mon, 12 Jan 2004 22:02:10 GMT
myDateTime.ToString("s") = 2004-01-12T22:02:10
myDateTime.ToString("t") = 10:02 PM
myDateTime.ToString("T") = 10:02:10 PM
myDateTime.ToString("u") = 2004-01-12 22:02:10Z
myDateTime.ToString("U") = January 13, 2004 6:02:10 AM
myDateTime.ToString("y") = January, 2004


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Date Time
» DateTime Format