In C#, you can format a DateTime
object to string using the ToString()
method with a format string. The format string consists of format specifiers that define how different parts of the date and time should be represented in the string.
Here is an example that demonstrates various ways to format a DateTime
object:
main.cs656 chars18 lines
In the example above, "MM/dd/yyyy"
represents the month (two digits), followed by the day (two digits), followed by the year (four digits). "hh:mm:ss tt"
represents the hour (two digits 12-hour clock), followed by the minute (two digits), followed by the second (two digits), followed by the AM/PM designator.
You can find more information on the available format specifiers in the official documentation: Custom date and time format strings.
gistlibby LogSnag