Timestamp Specifiers

Timestamps specify the intended hour of a day in two ways: 24-hour time (15:00) or 12-hour time (3:00 pm). Useful specifiers here are:

%H 00 Hour number (00--23), zero-padded to 2 digits
%I 12 Hour number in 12-hour clocks (01--12), zero-padded to 2 digits

%P am or pm in 12-hour clocks
%p AM or PM in 12-hour clocks

Timestamp strings can also specify how to deal with placeholder values in dates. The date July 8, 2020 can be written as 7/8/2020 or 07/08/2020, or even 07/08/20. The placeholding 0 is referred to as “zero-padding”. The alternative is known as “space-padding. So, for 12 vs 24 time, there are alternative specifiers for space-padding:

%k 0 Same as %H but space-padded. Same as %_H
%l 12 Same as %I but space-padded. Same as %_I

 

Years can be specified for four digits, the last two digits, or the first two digits. For the year 2019:
”2019”, use %Y - The full proleptic Gregorian year, zero-padded to 4 digits.

”19”, use %y - The proleptic Gregorian year modulo 100, zero-padded to 2 digits.
”20”, %C - The proleptic Gregorian year divided by 100, zero-padded to 2 digits.
%G 2001 Same as %Y but uses the year number in ISO 8601 week date. %g 01 Same as %y but uses the year number in ISO 8601 week date.

 

Months can be written in several ways - as numbers, padded numbers, and with their names. For July:

%m 07 Month number (01--12), zero-padded to 2 digits
%b Jul Abbreviated month name. Always 3 letters
%B July Full month name. Also accepts the corresponding abbreviation in parsing.
%h Jul Same as %b (Many specifiers have alternatives.)

Days also have names and numbers. For the 8th day of the month:

%d 08 Day number (01--31), zero-padded to 2 digits.
%_d. 8 Same as %d but space-padded. Same as %e.

%a Sun Abbreviated weekday name. Always 3 letters.
%A Sunday Full weekday name. Also accepts the corresponding abbreviation in parsing.

 

For days of the week, there are two systems:

%w 0 Sunday = 0, Monday = 1, ..., Saturday = 6.
%u 7 Monday = 1, Tuesday = 2, ..., Sunday = 7. (ISO 8601)

For the day of the year:

%j 189 Day of the year (001--366), zero-padded to 3 digits.

Weeks can be expressed as week number. With this transform, the source date will be used to return a value representing the week of the year.

%U 28 Week number starting with Sunday (00--53), zero-padded to 2 digits.
%W 27 Same as %U, but week 1 starts with the first Monday in that year instead.

%V 27 Same as %U but uses the week number in ISO 8601 week date (01--53). 

Some date formats are very commonly used, so they have their own shortcuts. With these, one character can specify a whole string:

%D 07/08/01 Month-day-year format. Same as %m/%d/%y.
%x 07/08/01 Same as %D.
%F 2001-07-08 Year-month-day format (ISO 8601). Same as %Y-%m-%d.
%v 8-Jul-2001 Day-month-year format. Same as %e-%b-%Y.

 

%c Sun Jul 8 00:34:60 2001 ctime date & time format. Same as %a %b %e %T %Y
%+ 2001-07-08T00:34:60.026490+09:30 ISO 8601 / RFC 3339 date & time format.

%s 994518299 UNIX timestamp, the number of seconds since 1970-01-01 00:00 UTC.

 

%H 00 Hour number (00--23), zero-padded to 2 digits.
%k 0 Same as %H but space-padded. Same as %_H.
%I 12 Hour number in 12-hour clocks (01--12), zero-padded to 2 digits.
%l 12 Same as %I but space-padded. Same as %_I.

%P am or pm in 12-hour clocks.
%p AM or PM in 12-hour clocks.

 

%M 34 Minute number (00--59), zero-padded to 2 digits.
%S 60 Second number (00--60), zero-padded to 2 digits.
%f 026490000 The fractional seconds (in nanoseconds) since last whole second.
%.f .026490 Similar to .%f but left-aligned.
%.3f .026 Similar to .%f but left-aligned but fixed to a length of 3.
%.6f .026490 Similar to .%f but left-aligned but fixed to a length of 6.
%.9f .026490000 Similar to .%f but left-aligned but fixed to a length of 9.

%R 00:34 Hour-minute format. Same as %H:%M.
%T 00:34:60 Hour-minute-second format. Same as %H:%M:%S.
%X 00:34:60 Same as %T.
%r 12:34:60 AM Hour-minute-second format in 12-hour clocks. Same as %I:%M:%S %p.

 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.