To format a DateTime
object in C#, you can use the ToString()
method with a format string.
Here are some common format specifiers:
d
or D
: short or long date patternt
or T
: short or long time patternf
or F
: full date/time pattern (short time)g
or G
: general date/time pattern (short time)s
or S
: sortable date/time pattern (ISO 8601)u
or U
: universal sortable date/time pattern (UTC Format)You can also use custom format strings to specify your own date/time format.
Here's an example of how to format a DateTime
object as a string using a predefined format specifier:
main.cs673 chars11 lines
And here's an example of how to use a custom format string:
main.cs93 chars2 lines
Keep in mind that the formatting options are case-sensitive, so d
and D
produce different results.
Note: The format specifiers listed above are just a few examples. For more information, you can refer to the official documentation on custom date and time format strings in C#:
gistlibby LogSnag