Skip to content

strftime

Collection Note

This module is part of the ansible.builtin collection. To install the collection, use:

ansible-galaxy collection install ansible.builtin
Added in version 2.4.

Synopsis

  • Using Python's C(strftime) function, take a data formating string and a date/time to create a formatted date.

Parameters

Parameter Defaults / Choices Comments
_input
str
required
A formating string following C(stftime) conventions.
See L(the Python documentation, https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior) for a reference.
second
int
Datetime in seconds from C(epoch) to format, if not supplied C(gmttime/localtime) will be used.
utc
bool
Whether time supplied is in UTC.
Version Added: 2.14

Notes

Note

  • This is a passthrough to Python's C(stftime), for a complete set of formatting options go to https://strftime.org/.

Examples

# for a complete set of features go to  https://strftime.org/

# Display year-month-day
{{ '%Y-%m-%d' | strftime }}
# => "2021-03-19"

# Display hour:min:sec
{{ '%H:%M:%S' | strftime }}
# => "21:51:04"

# Use ansible_date_time.epoch fact
{{ '%Y-%m-%d %H:%M:%S' | strftime(ansible_date_time.epoch) }}
# => "2021-03-19 21:54:09"

# Use arbitrary epoch value
{{ '%Y-%m-%d' | strftime(0) }}          # => 1970-01-01
{{ '%Y-%m-%d' | strftime(seconds=1441357287, utc=true) }} # => 2015-09-04

Return Values

Key Data Type Description Returned
_value str A formatted date/time string.