Chapter 10. .NET DateTime Formatting

DateTime format strings convert a DateTime object to one of several standard formats, as listed in Table 10-1.

Table 10-1. Standard DateTime format strings

Format specifier (Name)

Description

Example

d(Short date)

The culture’s short date format.

PS >"{0:d}" -f [DateTime] "01/23/4567"
1/23/4567

D (Long date)

The culture’s long date format.

PS >"{0:D}" -f [DateTime] "01/23/4567"
Friday, January 23, 4567

f (Full date/ short time)

Combines the long date and short time format patterns.

PS >"{0:f}" -f [DateTime] "01/23/4567"
Friday, January 23, 4567 12:00 AM

F (Full date/ long time)

Combines the long date and long time format patterns.

PS >"{0:F}" -f [DateTime] "01/23/4567"
Friday, January 23, 4567 12:00:00 AM

g (General date/ short time)

Combines the short date and short time format patterns.

PS >"{0:g}" -f [DateTime] "01/23/4567"
1/23/4567 12:00 AM

G (General date/ long time)

Combines the short date and long time format patterns.

PS >"{0:G}" -f [DateTime] "01/23/4567"
1/23/4567 12:00:00 AM

M or m(Month day)

The culture’s MonthDay format.

PS >"{0:M}" -f [DateTime] "01/23/4567"
January 23

o (Round-trip date/time)

The date formatted with a pattern that guarantees the string (when parsed) will result in the original DateTime again.

PS >"{0:o}" -f [DateTime] "01/23/4567"
4567-01-23T00:00:00.0000000

R or r (RFC1123)

The standard RFC1123 format pattern.

PS >"{0:R}" -f [DateTime] "01/23/4567"
Fri, 23 Jan 4567 00:00:00 GMT

s (Sortable)

Sortable format pattern. Conforms ...

Get Windows PowerShell Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.