format a date in to the format month day, year hour:minute am/pm in csharp

In C#, you can format a date using the ToString() method and specifying a format string. To format the date in the format "month day, year hour:minute am/pm", you can use the following format string:

main.cs
dateTime.ToString("MMMM dd, yyyy h:mm tt")
43 chars
2 lines

Here, "MMMM" represents the full month name, "dd" represents the zero-padded day of the month, "yyyy" represents the four-digit year, "h" represents the hour in 12-hour format, "mm" represents the zero-padded minute, and "tt" represents the AM/PM designator.

For example, if you have a DateTime object date that represents March 14, 2022 at 1:30 PM, you can format it like this:

main.cs
DateTime date = new DateTime(2022, 3, 14, 13, 30, 0);
string formattedDate = date.ToString("MMMM dd, yyyy h:mm tt");
Console.WriteLine(formattedDate); // Output: "March 14, 2022 1:30 PM"
187 chars
4 lines

gistlibby LogSnag