strftime Cheat Sheet
The strftime
codes are widely used in programming languages like C ꜛ, C++ ꜛ, PHP ꜛ, Ruby ꜛ, Python ꜛ or on Jekyll websites by using Liquid tags. With the appropriate platform specific function, strftime
codes format date and time into strings, according to the specified format placeholders listed below. These strings can then be used, e.g., to print out dates and times in the desired output format.
The strftime
codes are based on the 1989 C standard ꜛ and, hence, work on all platforms with a standard C implementation.
The 1999 C standard added additional format codes: By appending a dash -
(UNIX) or hash #
(Windows) after the percent %
sign, the format code will print out the corresponding number without leading zeroes. Depending on the underlying C implementation, these add-ons might not work on every platform.
Online generators like strftime.net ꜛ or strfti.me ꜛ help to find the corresponding placeholders for your desired date and time string format.
Pre-formatted date and time strings
Placeholder | Example | Description |
---|---|---|
%c |
Thu Jan 28 12:32:01 2014 | locale’s appropriate date and time representation |
%D |
23/05/12 | formats the date |
%F |
2002-01-30 | date in ISO 8601 format YYYY-MM-DD |
%x |
02/10/11 | locale’s appropriate date representation |
%X |
14:22:01 | locale’s appropriate time representation |
%r |
3:44:12 AM | 12-hour time |
%R |
15:21 | 24-hour time HH:MM |
%T |
15:21:59 | time in ISO 8601 format HH:MM:SS |
Individual date and time placeholders
Placeholder | Example | Description |
---|---|---|
%A |
Monday | full weekday name |
%a |
Mon | abbreviated weekday name |
%w |
0-6 | day of the week with Sunday as 0 |
%d |
01-31 | day of the month (with a leading zero) |
%e |
1-31 | day of the month (without a leading zero) |
%B |
April | full month name |
%b |
Apr | abbreviated month name |
%m |
01-12 | month of the year (with a leading zero) |
%-m |
1-12 | month of the year (without a leading zero) |
%Y |
2003 | year |
%y |
00-99 | year without a century (last two digits, with a leading zero) |
%-y |
0-99 | year without a century (last two digits, without a leading zero) |
%H |
00-23 | hour of the day, 24-hour time (with a leading zero) |
%k |
0-23 | hour of the day, 24-hour time (without a leading zero) |
%I |
01-11 | hour of the day, 12-hour time (with a leading zero) |
%-I |
1-11 | hour of the day, 12-hour time (without a leading zero) |
%P |
am, pm | am or pm designation |
%p |
AM, PM | AM or PM designation |
%M |
00-59 | minute of the hour (with a leading zero) |
%-M |
0-59 | minute of the hour (without a leading zero) |
%S |
00-60 | second of the minute (with a leading zero) |
%-S |
0-60 | second of the minute (without a leading zero) |
%f |
000000-999999 | microsecond of the second (with a leading zero) |
%Z |
UTC | timezone name or abbreviation |
%z |
+0000 | UTC offset in the form +HHMM or -HHMM |
%s |
amount of seconds since 1970-01-01 00:00:00 UTC | |
%% |
% sign |
Weeke numbers and day of the year
Placeholder | Example | Description |
---|---|---|
%j |
001-366 | day of the year (with a leading zeroes) |
%U |
00-53 | week number with the first Sunday as the first day of week one |
%W |
00-53 | week number of the current year, starting with the first Monday as the first day of the first week |
%V |
01-53 | week number in ISO 8601 format |
Examples
Liquid on Jekyll websites
{{ post.date | date: "%a, %b %d, %y" }}
Thur, Aug 17, 21
{{ post.date | date: "%Y" }}
2021
{{ "May 4, 2020" | date: "%a, %b %d, %y" }}
Thur, May 4, 20
You can also work with the current time and date:
{{ "now" | date: "%Y-%m-%d %H:%M" }}
2021-08-11 20:15
You can find further date and time format options on the Liquid Cheat Sheet.
Python
from datetime import datetime
# retrieve the current date and time:
now = datetime.now()
year = now.strftime("%Y")
print("year:", year)
month = now.strftime("%m")
print("month:", month)
day = now.strftime("%d")
print("day:", day)
time = now.strftime("%H:%M:%S")
print("time:", time)
date_time = now.strftime("%Y-%d-%mT%H:%M:%S")
print("date and time stamp:",date_time)
year: 2021
month: 08
day: 15
date and time stamp: 2021-08-15T13:43:09
References
- C++ reference on
strftime
ꜛ - Wikipedia article on C date and time functions ꜛ
- Linux manual page on
strftime(3)
ꜛ - Python documentation on
strftime()
andstrptime()
Format Codes ꜛ - C documentation on
strftime()
ꜛ - C++ documentation on
strftime()
ꜛ - Ruby documentation on
strftime()
ꜛ - PHP documentation on
strftime()
ꜛ - strftime.org ꜛ
- Online generators:
comments