C# DateTime ToString()

C# DateTime ToString()

C# DateTime ToString format method. Here are examples which cover all type of string representation of date into other formats of Date String.
Let's see them one by one. DateTime Instace.ToString(“d”) returns only date part.

DateTime to string [C#] Example.

C#-Format
 using System;
namespace DateAndTime.Examples
{
class Program
{
static void Main(string[] args)
{
DateTime dt = DateTime.Parse("01/30/2012 11:03:05 PM");
Console.WriteLine("Full Date To string: dt.ToString() = " + dt.ToString());
Console.WriteLine("Extract only date part from date: dt.ToString(\"d\") = " + dt.ToString("d"));
Console.WriteLine("Extract date with month in words: dt.ToString(\"D\") = " + dt.ToString("D"));
Console.WriteLine("Long date with month in words: dt.ToString(\"f\") = " + dt.ToString("f"));
Console.WriteLine("Long date with month in words: dt.ToString(\"F\") = " + dt.ToString("F"));
Console.WriteLine("Date with time string: dt.ToString(\"g\") = " + dt.ToString("g"));
Console.WriteLine("Date with time in seconds string: dt.ToString(\"G\") = " + dt.ToString("G"));
Console.WriteLine("Month and year string: dt.ToString(\"m\") = " + dt.ToString("m"));
Console.WriteLine("Full date in words: dt.ToString(\"r\") = " + dt.ToString("r"));
Console.WriteLine("dt.ToString(\"s\") = " + dt.ToString("s"));
Console.WriteLine("dt.ToString(\"t\") = " + dt.ToString("t"));
Console.WriteLine("dt.ToString(\"T\") = " + dt.ToString("T"));
Console.WriteLine("dt.ToString(\"u\") = " + dt.ToString("u"));
Console.WriteLine("dt.ToString(\"U\") = " + dt.ToString("U"));
Console.WriteLine("dt.ToString(\"y\") = " + dt.ToString("y"));
Console.ReadLine();
}
}
}
c# Date Format

Vb.Net example of DateTime to String format

Namespace DateAndTime.Examples
Class Program
Private Shared Sub Main(args As String())
Dim dt As DateTime = DateTime.Parse("01/30/2012 11:03:05 PM")
Console.WriteLine("Full Date To string: dt.ToString() = " & dt.ToString())
Console.WriteLine("Extract only date part from date: dt.ToString(""d"") = " & dt.ToString("d"))
Console.WriteLine("Extract date with month in words: dt.ToString(""D"") = " & dt.ToString("D"))
Console.WriteLine("Long date with month in words: dt.ToString(""f"") = " & dt.ToString("f"))
Console.WriteLine("Long date with month in words: dt.ToString(""F"") = " & dt.ToString("F"))
Console.WriteLine("Date with time string: dt.ToString(""g"") = " & dt.ToString("g"))
Console.WriteLine("Date with time in seconds string: dt.ToString(""G"") = " & dt.ToString("G"))
Console.WriteLine("Month and year string: dt.ToString(""m"") = " & dt.ToString("m"))
Console.WriteLine("Full date in words: dt.ToString(""r"") = " & dt.ToString("r"))
Console.WriteLine("dt.ToString(""s"") = " & dt.ToString("s"))
Console.WriteLine("dt.ToString(""t"") = " & dt.ToString("t"))
Console.WriteLine("dt.ToString(""T"") = " & dt.ToString("T"))
Console.WriteLine("dt.ToString(""u"") = " & dt.ToString("u"))
Console.WriteLine("dt.ToString(""U"") = " & dt.ToString("U"))
Console.WriteLine("dt.ToString(""y"") = " & dt.ToString("y"))
Console.ReadLine()
End Sub
End Class
End Namespace

DateTime other examples

How to format Time in C#.net and VB.net program.
DateTime Time Format string {HH:mm:ss}.
C#.net date time Culture.
C#.net DateTime Month.
string was not recognized as a valid date time
Get Today
DateTime in English UK format
Get Day Of year
DateTime.Today vs. DateTime.Now
DateTime Parse Exact method.
Parse Exact DateTime
DateTime Get difference in days, minute, seconds

Tags:

DateTimeTryParse using C# and VB.net exampleC# DateTime DifferenceC# Parse method of DateTimeC# DateTimeDateTime.Today Vs DateTime.NowDateTimeTryParse with IFormatProvider culture information using c#IFormatProvider DateTime C#.netC# Convert String To DateTimeC# Datetime MonthDateTime in C# current Date and TimeAsp.net Cache Object

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.