The strftime()
function in Python is a method of the datetime
class that converts a datetime object to a string representation. The method takes a format string as an argument and returns a string representing the date and time in the specified format.
Here’s the basic syntax for using strftime()
:
datetime_object.strftime(format)
The datetime_object
is the datetime object that you want to convert to a string, and the format
argument is a string that specifies the format of the output. The format
string can include various placeholders (such as %Y
for the year, %m
for the month, %d
for the day, %H
for the hour, %M
for the minute, %S
for the second, etc.) that will be replaced by the appropriate values from the datetime object.
For example, the following code shows how to use strftime()
to format a datetime object as a string:
import datetime now = datetime.datetime.now() formatted_date = now.strftime("%Y-%m-%d %H:%M:%S") print("Current date and time:", formatted_date)
In this example, the %Y-%m-%d %H:%M:%S
format string is used to display the current date and time in the format YYYY-MM-DD HH:MM:SS
. The output of the above code might look something like this:
Current date and time: 2023-03-22 14:23:45
There are many other formatting options available in the strftime()
function, which you can find in the Python documentation.
Using Python strftime() function along with a predefined timestamp:
You can use the strftime()
function in Python to format a predefined timestamp as a string in a particular format.
Here’s an example of how you can do this:
import datetime # Define a timestamp timestamp = 1648039378 # Convert the timestamp to a datetime object datetime_object = datetime.datetime.fromtimestamp(timestamp) # Format the datetime object as a string in a particular format formatted_date = datetime_object.strftime("%Y-%m-%d %H:%M:%S") # Print the formatted date print("Formatted date:", formatted_date)
In this example, we first define a timestamp (in this case, 1648039378
). We then convert this timestamp to a datetime
object using the fromtimestamp()
method. Next, we use the strftime()
method to format the datetime
object as a string in the format specified by the %Y-%m-%d %H:%M:%S
format string. Finally, we print the formatted date.
The output of this code will be:
Formatted date: 2022-03-22 06:56:18
Note that the strftime()
function can be used with any datetime
object, not just those generated from timestamps.
Using various Format codes with Python strftime() function:
Here are some commonly used format codes that you can use with the Python strftime()
function to format a datetime object:
%Y
: Year with century as a decimal number.%m
: Month as a zero-padded decimal number (01-12).%d
: Day of the month as a zero-padded decimal number (01-31).%H
: Hour (24-hour clock) as a zero-padded decimal number (00-23).%M
: Minute as a zero-padded decimal number (00-59).%S
: Second as a zero-padded decimal number (00-59).%f
: Microsecond as a decimal number (000000-999999).%a
: Weekday abbreviated name (e.g. Mon, Tue, etc.).%A
: Weekday full name (e.g. Monday, Tuesday, etc.).%b
: Month abbreviated name (e.g. Jan, Feb, etc.).%B
: Month full name (e.g. January, February, etc.).%c
: Locale’s appropriate date and time representation.%p
: AM or PM designation.%z
: UTC offset in the form +HHMM or -HHMM.%Z
: Time zone name (empty string if the object is naive).%j
: Day of the year as a zero-padded decimal number (001-366).%U
: Week number of the year (Sunday as the first day of the week) as a zero-padded decimal number (00-53).%W
: Week number of the year (Monday as the first day of the week) as a zero-padded decimal number (00-53).%x
: Locale’s appropriate date representation.%X
: Locale’s appropriate time representation.%%
: A literal ‘%’ character.
You can use these format codes in any combination to format a datetime object into a string. For example, the following code formats the current date and time into a string with the format YYYY-MM-DD HH:MM:SS
:
import datetime now = datetime.datetime.now() formatted_date = now.strftime("%Y-%m-%d %H:%M:%S") print("Current date and time:", formatted_date)
The output of this code might look something like this:
Current date and time: 2023-03-22 14:23:45
You can find more information about format codes and their usage in the official Python documentation.