format a date in to the format october 13, 2014 11:13:00.000 am in csharp

You can achieve this format in C# by using the ToString() method on a DateTime object with a custom format string.

main.cs
DateTime dateTime = new DateTime(2014, 10, 13, 11, 13, 0, 0);
string formattedDateTime = dateTime.ToString("MMMM dd, yyyy h:mm:ss.fff tt");
Console.WriteLine(formattedDateTime);
178 chars
4 lines

Output:

main.cs
October 13, 2014 11:13:00.000 AM
33 chars
2 lines

In the format string:

  • MMMM represents the full month name
  • dd represents the numeric day of the month (with leading zero)
  • yyyy represents the full year
  • h represents the 12-hour clock hour (without leading zero)
  • mm represents the minute (with leading zero)
  • ss represents the second (with leading zero)
  • fff represents the milliseconds (with leading zeros if necessary)
  • tt represents the AM/PM designator

You can find more information about custom date and time format strings in the Microsoft documentation: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

gistlibby LogSnag