Appendix E. .NET DateTime Formatting

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

Table E-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 to ISO 8601 and provides output suitable for sorting.

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

t

Short time

The culture’s ShortTime format.

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

T

Long time

The culture’s LongTime format.

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

u

Universal sortable

The culture’s UniversalSortable DateTime format applied to the UTC equivalent of the input.

PS > "{0:u}" -f [DateTime] "01/23/4567"
4567-01-23 00:00:00Z

U

Universal

The culture’s FullDateTime format applied to the UTC equivalent of the input.

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

Y or y

Year month

The culture’s YearMonth format.

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

Custom DateTime Format Strings

You can use the custom DateTime format strings listed in Table E-2 to format dates in ways not supported by the standard format strings.

Note

Single-character format specifiers are by default interpreted as a standard DateTime formatting string unless they are used with other formatting specifiers. Add the % character before them to have them interpreted as a custom format specifier.

Table E-2. Custom DateTime format strings

Format specifier

Description

Example

d

Day of the month as a number between 1 and 31. Represents single-digit days without a leading zero.

PS > "{0:%d}" -f
    [DateTime] "01/02/4567"
2

dd

Day of the month as a number between 1 and 31. Represents single-digit days with a leading zero.

PS > "{0:dd}" -f
    [DateTime] "01/02/4567"
02

ddd

Abbreviated name of the day of week.

PS > "{0:ddd}" -f
    [DateTime] "01/02/4567"
Fri

dddd

Full name of the day of the week.

PS > "{0:dddd}" -f
    [DateTime] "01/02/4567"
Friday

f

Most significant digit of the seconds fraction (milliseconds).

PS > $date = Get-Date
PS > $date.Millisecond
93
PS > "{0:%f}" -f $date
0

ff

Two most significant digits of the seconds fraction (milliseconds).

PS > $date = Get-Date
PS > $date.Millisecond
93
PS > "{0:ff}" -f $date
09

fff

Three most significant digits of the seconds fraction (milliseconds).

PS > $date = Get-Date
PS > $date.Millisecond
93
PS > "{0:fff}" -f $date
093

ffff

Four most significant digits of the seconds fraction (milliseconds).

PS > $date = Get-Date
PS > $date.Millisecond
93
PS > "{0:ffff}" -f $date
0937

fffff

Five most significant digits of the seconds fraction (milliseconds).

PS > $date = Get-Date
PS > $date.Millisecond
93
PS > "{0:fffff}" -f $date
09375

ffffff

Six most significant digits of the seconds fraction (milliseconds).

PS > $date = Get-Date
PS > $date.Millisecond
93
PS > "{0:ffffff}" -f $date
093750

fffffff

Seven most significant digits of the seconds fraction (milliseconds).

PS > $date = Get-Date
PS > $date.Millisecond
93
PS > "{0:fffffff}" -f $date
0937500

F

FF

FFF

(...)

FFFFFFF

Most significant digit of the seconds fraction (milliseconds).

When compared to the lowercase series of 'f' specifiers, displays nothing if the number is zero.

PS > "{0:|F FF FFF FFFF|}" -f 
    [DateTime] "01/02/4567"
|   |

%g or gg

Era (e.g., A.D.).

PS > "{0:gg}" -f [DateTime]
    "01/02/4567"
A.D.

%h

Hours, as a number between 1 and 12. Single digits do not include a leading zero.

PS > "{0:%h}" -f
    [DateTime] "01/02/4567 4:00pm"
4

hh

Hours, as a number between 01 and 12. Single digits include a leading zero. Note: This is interpreted as a standard DateTime formatting string unless used with other formatting specifiers.

PS > "{0:hh}" -f
    [DateTime] "01/02/4567 4:00pm"
04

%H

Hours, as a number between 0 and 23. Single digits do not include a leading zero.

PS > "{0:%H}" -f
    [DateTime] "01/02/4567 4:00pm"
16

HH

Hours, as a number between 00 and 23. Single digits include a leading zero.

PS > "{0:HH}" -f
     [DateTime] "01/02/4567 4:00am"
04

K

DateTime.Kind specifier that corresponds to the kind (i.e., Local, Utc, or Unspecified) of input date.

PS > "{0:%K}" -f
    [DateTime]::Now.ToUniversalTime()
Z

m

Minute, as a number between 0 and 59. Single digits do not include a leading zero.

PS > "{0:%m}" -f [DateTime]::Now
 7

mm

Minute, as a number between 00 and 59. Single digits include a leading zero.

PS > "{0:mm}" -f [DateTime]::Now
08

M

Month, as a number between 1 and 12. Single digits do not include a leading zero.

PS > "{0:%M}" -f
    [DateTime] "01/02/4567"
1

MM

Month, as a number between 01 and 12. Single digits include a leading zero.

PS > "{0:MM}" -f
    [DateTime] "01/02/4567"
01

MMM

Abbreviated month name.

PS > "{0:MMM}" -f
    [DateTime] "01/02/4567"
Jan

MMMM

Full month name.

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

s

Seconds, as a number between 0 and 59. Single digits do not include a leading zero.

PS > $date = Get-Date
PS > "{0:%s}" -f $date
7

ss

Seconds, as a number between 00 and 59. Single digits include a leading zero.

PS > $date = Get-Date
PS > "{0:ss}" -f $date
07

t

First character of the a.m./p.m. designator.

PS > $date = Get-Date
PS > "{0:%t}" -f $date
P

tt

a.m./p.m. designator.

PS > $date = Get-Date
PS > "{0:tt}" -f $date
PM

y

Year, in (at most) two digits.

PS > "{0:%y}" -f 
    [DateTime] "01/02/4567"
67

yy

Year, in (at most) two digits.

PS > "{0:yy}" -f 
    [DateTime] "01/02/4567"
67

yyy

Year, in (at most) four digits.

PS > "{0:yyy}" -f 
    [DateTime] "01/02/4567"
4567

yyyy

Year, in (at most) four digits.

PS > "{0:yyyy}" -f
    [DateTime] "01/02/4567"
4567

yyyyy

Year, in (at most) five digits.

PS > "{0:yyyy}" -f
    [DateTime] "01/02/4567"
04567

z

Signed time zone offset from GMT. Does not include a leading zero.

PS > "{0:%z}" -f [DateTime]::Now
-8

zz

Signed time zone offset from GMT. Includes a leading zero.

PS > "{0:zz}" -f [DateTime]::Now
-08

zzz

Signed time zone offset from GMT, measured in hours and minutes.

PS > "{0:zzz}" -f [DateTime]::Now
-08:00

:

Time separator.

PS > "{0:y/m/d h:m:s}" -f
    [DateTime] "01/02/4567 4:00pm"
67/0/2 4:0:0

/

Date separator.

PS > "{0:y/m/d h:m:s}" -f
    [DateTime] "01/02/4567 4:00pm"
67/0/2 4:0:0

"text"

'text'

Inserts the provided text literally into the output without affecting formatting.

PS > "{0:'Day: 'dddd}" -f
    [DateTime]::Now
Day: Monday

%c

Syntax allowing for single-character custom formatting specifiers. The % sign is not added to the output.

PS > "{0:%h}" -f
    [DateTime] "01/02/4567 4:00pm"
4

Other

Inserts the provided text literally into the output without affecting formatting.

PS > "{0:dddd!}" -f [DateTime]::Now
Monday!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.80.34