Date and time formatting
Format date and time values with text patterns that define the elements of the timestamp.
Patterns can be used for output to define how the timestamp should appear, or input to define the format of the string to convert to a Date/time value.
A typical pattern is "yyyy-MM-dd HH:mm", which creates a formatted timestamp like 2013-03-05 19:52.
Date and time formatting pattern elements
These symbols are the pattern elements that define date/time formats.
|
Symbol |
Meaning |
Example pattern |
Result |
|---|---|---|---|
|
G |
Era |
G |
AD |
|
C |
Century of era |
C |
20 |
|
y, Y, or x |
Year |
y |
2003 |
|
M |
Month |
M |
1 |
|
d |
Day in month |
d |
3 |
|
h |
Hour (1-12) |
h |
7 |
|
H |
Hour (0-23) |
H |
23 |
|
k |
Hour (1-24) |
k |
3 |
|
K |
Hour (0-11) |
K |
0 |
|
a |
AM/PM |
a |
AM |
|
m |
Minute |
m |
7 |
|
s |
Second |
s |
2 |
|
S |
Millisecond (0-9999) |
SSS |
007 |
|
E |
Day in week, name |
E |
Tue |
|
e |
Day in week, number (Monday=1) |
e |
2 |
|
D |
Day in year (1-365 or 1-366) |
D |
65 |
|
w |
Week in year (1-53) |
w |
7 |
|
z |
Time zone |
z |
PST |
|
Z |
Time zone offset/id |
Z |
-0700 |
|
' |
Escape for text |
'hour' h |
hour 9 |
|
'' |
Single quote (x2 single-quote chars) |
ss''SSS |
45'876 |
Here are some examples based on the timestamp: 2001-07-04 12:08:56 local time in the U.S. Pacific Time time zone.
|
Date and Time Pattern |
Result |
|---|---|
|
"yyyy.MM.dd G 'at' HH:mm:ss z" |
2001.07.04 AD at 12:08:56 PDT |
|
"EEE, MMM d, ''yy" |
Wed, Jul 4, '01 |
|
"h:mm a" |
12:08 PM |
|
"hh 'o''clock' a, zzzz" |
12 o'clock PM, Pacific Daylight Time |
|
"K:mm a, z" |
0:08 PM, PDT |
|
"yyyyy.MMMMM.dd GGG hh:mm aaa" |
02001.July.04 AD 12:08 PM |
|
"EEE, d MMM yyyy HH:mm:ss Z" |
Wed, 4 Jul 2001 12:08:56 -0700 |
|
"yyMMddHHmmssZ" |
010704120856-0700 |
|
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" |
2001-07-04T12:08:56.235-0700 |
