English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python strftime()

In this article, you will learn how to convert date, time, and datetime objects into their equivalent strings (through examples)

strftime() method usesdate,timeordatetimeThe object returns a string representing the date and time.

Example1Use strftime() to convert date and time to string

The following program will convert the datetime object containing the current date and time into different string formats.

from datetime import datetime
now = datetime.now()  # Current date and time
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("%m")/%d/d = date_time.strftime("%Y, %H:%M:%S")
print("Date and time:", date_time)

When you run the program, the output will be as follows:

Year: 2020
Month: 04
Day: 13
Time: 17:35:22
Date and time: 04/13/2020, 17:35:22

Here,year,day,timeanddate_timeis a string, whilenowis a datetime object.

How does strftime() work?

In the above program, %Y, %m, %d, etc. are format codes. The strftime() method takes one or more format codes as parameters and returns a formatted string based on these codes.

  1. We imported the class from the datetime module. This is because the datetime class object can access the strftime() method.

     

  2. The datetime object containing the current date and time is stored innowin

     

  3. The strftime() method can be used to create a formatted string.

     

  4. The string you pass to the strftime() method may contain multiple format codes.

     

Example2: Create a string based on the timestamp

from datetime import datetime
timestamp = 1578797322
date_time = datetime.fromtimestamp(timestamp)
print("DateTime object:", date_time)
d = date_time.strftime("%m/%d/d = date_time.strftime("%Y, %H:%M:%S")
print("Output 2: "	
d = date_time.strftime("%d %b, %Y")
print("Output 3: "
d = date_time.strftime("%d %B, %Y")
print("Output 4: "
d = date_time.strftime("%I%p")
print("Output 5: "

When running the program, the output is:

DateTime object: 2020-01-12 10:48:42
Output 2: 01/12/2020, 10:48:42
Output 3: 12 Jan, 2020
Output 4: 12 January, 2020
Output 5: 10AM

List of format codes

The following table shows all the codes you can pass to the strftime() method.

CommandMeaningFor example
%aAbbreviation of the workday name.Sun, Mon, ...
%AFull name of the workday.Sunday, Monday, ..
%wWorkday represented as a decimal number.0,1, ... ,6
%dA day of the month, zero-padded as a decimal number.01, 02, ... ,31
%-dA day of the month represented as a decimal number.1,2, ... ,30
%bAbbreviation of the month.Jan, Feb, ..., Dec
%BComplete month name.January, February, ...
%mThe month, zero-padded as a decimal number.01、02, ... ,12
%-mThe month represented as a decimal number.1,2, ... ,12
%yA year without centuries, zero-padded as a decimal number.00, 01, ... ,99
%-yA year without centuries, represented as a decimal number.0,1, ... ,99
%YA year represented as a decimal number with centuries.2013,2019and
%HHour (24Hour-based), a zero-padded decimal number.00, 01, ... ,23
%-HHour (24(12-hour clock) as a decimal number.0,1, ... ,23
%IHour (12Hour-based), a zero-padded decimal number.01、02, ... ,12
%-IHour (12(12-hour clock) as a decimal number.1 2 2
%pThe appropriate morning or afternoon in the language environment.AM, PM
%MMinute, a decimal number with leading zeros.00, 01, ... ,59
%-MRepresented by a decimal number.0,1, ... ,59
%SThe second decimal number with leading zeros.00, 01, ... ,59
%-SThe second decimal digit.0,1, ... ,59
%fMicroseconds, a decimal number, padded with zeros on the left.000000-999999
%zThe UTC offset, formatted as+ HHMM or-HHMM. 
%ZThe time zone name. 
%jThe day of the year, represented by a decimal number with leading zeros.001, 002, ... ,366
%-jThe day of the year, represented by a decimal number.1,2, ... ,366
%UThe week number of the year (Sunday is the first day of the week). All days before the first Sunday of a new year are considered to be in the 0th week.00, 01, ... ,53
%WThe week number of the year (Monday is the first day of the week). All days before the first Monday of a new year are considered to be in the 0th week.00, 01, ... ,53
%cThe appropriate date and time representation in the language environment.Mon Sep 30 07: 06: 05 2019
%xThe appropriate date representation in the language environment.13/9/30
%XThe appropriate time representation in the language environment.07: 06: 05
%%The character "%".%

Example3: appropriate date and time in the language environment

from datetime import datetime
timestamp = 1578797322
date_time = datetime.fromtimestamp(timestamp)
d = date_time.strftime("%c")
print("Output 1: "	
d = date_time.strftime("%x")
print("Output 2: "
d = date_time.strftime("%X")
print("Output 3: "

When running the program, the output is:

Output 1: Sun Jan 12 10:48:42 2020
Output 2: 01/12/20
Output 3: 10:48:42

Format codes %c, %x, and %X are used for the appropriate date and time representation in the language environment.

We also recommend that you checkPython strptime()The .strptime() method creates a datetime object from a string.