use the ToString() method to convert a DateTime to a string
using System;
class MainClass {
public static void Main() {
DateTime myDateTime12 = new DateTime(2004, 1, 12, 22, 2, 10);
Console.WriteLine("myDateTime12.ToString() = " + myDateTime12.ToString());
Console.WriteLine("myDateTime12.ToString(\"MMMM dd, yyyy\") = " +myDateTime12.ToString("MMMM dd, yyyy"));
Console.WriteLine("myDateTime12.ToString(\"d\") = " + myDateTime12.ToString("d"));
Console.WriteLine("myDateTime12.ToString(\"D\") = " + myDateTime12.ToString("D"));
Console.WriteLine("myDateTime12.ToString(\"f\") = " + myDateTime12.ToString("f"));
Console.WriteLine("myDateTime12.ToString(\"F\") = " + myDateTime12.ToString("F"));
Console.WriteLine("myDateTime12.ToString(\"g\") = " + myDateTime12.ToString("g"));
Console.WriteLine("myDateTime12.ToString(\"G\") = " + myDateTime12.ToString("G"));
Console.WriteLine("myDateTime12.ToString(\"m\") = " + myDateTime12.ToString("m"));
Console.WriteLine("myDateTime12.ToString(\"r\") = " + myDateTime12.ToString("r"));
Console.WriteLine("myDateTime12.ToString(\"s\") = " + myDateTime12.ToString("s"));
Console.WriteLine("myDateTime12.ToString(\"t\") = " + myDateTime12.ToString("t"));
Console.WriteLine("myDateTime12.ToString(\"T\") = " + myDateTime12.ToString("T"));
Console.WriteLine("myDateTime12.ToString(\"u\") = " + myDateTime12.ToString("u"));
Console.WriteLine("myDateTime12.ToString(\"U\") = " + myDateTime12.ToString("U"));
Console.WriteLine("myDateTime12.ToString(\"y\") = " + myDateTime12.ToString("y"));
}
}
Related examples in the same category