Table of Contents
This updated guide examines Handle Time in Python: Strftime ()() Function in Python and organizes the essential facts, background, and practical takeaways in clear American English.
The strftime () function in Python returns a string representing the date, time, and time values using date, time, and datetime objects.

Example 1: Use strftime () to convert datetime to string
The following program converts a 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/%Y, %H:%M:%S") print("date and time:",date_time)
Run the program, the result is:
year: 2019 month: 07 day: 10 time: 10:37:16 date and time: 07/10/2019, 10:37:16
You can see, the variables date, time, and datetime here are in the form of string, only the now variable in the form of datetime object
How does strftime () work?
In the above program,% Y,% m,% d. are format codes. The strftime function () takes that format code as an argument and returns a string formatted based on it.
1. The datetime class is imported from the datetime module because the object of the datetime class can access the strftime function ().
2. The datetime object contains the current date and time stored in the now variable .
3. The strftime () function is used to create strings according to the format we passed.
4. The string you pass into the strftime function () may contain a lot of formatting codes.
Example 2: Use timestamp value
from datetime import datetime timestamp = 1562733155 date_time = datetime.fromtimestamp(timestamp) print("Thoi gian tuong ung:", date_time) d = date_time.strftime("%m/%d/%Y, %H:%M:%S") print("Output 2:", d) d = date_time.strftime("%d %b, %Y") print("Output 3:", d) d = date_time.strftime("%d %B, %Y") print("Output 4:", d) d = date_time.strftime("%I%p") print("Output 5:", d)
Run the program, the result is:
Thoi gian tuong ung: 2019-07-10 11:32:35 Output 2: 07/10/2019, 11:32:35 Output 3: 10 Jul, 2019 Output 4: 10 July, 2019 Output 5: 11AM
List of code formats
The table below shows all the formatting codes that you can pass to the strftime method ().
SignMeaningFor example %aAbbreviated name of the week Sun, Mon,.%Aname of the week in the week that is written in full Sunday, Monday,.%wDay of the week, value form 0, 1,., 6%dDay of the month, numeric value (value 0 as a buffer before the 1-digit date) 01, 02,., 31%-dDate of the month, value type 1, 2,.., 30%bAbbreviated month name Jan, Feb,., Dec%BFull month name, January, February,.%mMonth of the year, numeric form (value 0 as previous buffer month with 1 digit) 01, 02,., 12%-mMonth of the year, value form 1, 2,., 12%y2-digit year value (value 0 as buffer 1 digit before the year) 00, 01,., 99%-yYear 2 digit values 0, 1,., 99%YFull year value 2013, 2019.%HHour in a 24 hour system (with a value of 0 as a buffer before 1 digit) 00, 01,., 23%-H24 hour system time, value form number 0, 1,., 23%I12 hour system time, numerical value form (value 0 as buffer before 1 digit) 01, 02,., 12%-I12 hour hours 1, 2,. 12%pLocal time is AM or PM. AM, PM%MMinutes, numeric values (value 0 as 1-minute pre-minute buffers) 00, 01,., 59%-MMinutes, numeric values 0, 1,., 59%SSeconds, numeric values (value 0 as a buffer before the 1-digit second) 00, 01,., 59%-SSeconds, numeric values 0, 1,., 59%fMicro seconds, numeric value (value 0 as a 1-second pre-pad buffer) 000000 - 999999%zUTC compensation time in + HHMM or -HHMM format.%ZTime zone name%jDate of the year, numeric value (value 0, 00 as a buffer before the date has 1 and 2 digits) 001, 002,., 366%-jYear of the year, form values of 1, 2,., 366%UNumber of weeks in the year (Sunday is the first day of the week). All days in the new year before the first Sunday are considered as during the week of 00, 01,., 53%WNumber of weeks in the year (Monday is the first day of the week). All days in the new year before the first Monday are considered in week 0. 00, 01,., 53%cReturns the date and time Sep 30 30:06:05 2013%xReturns on 09 / 30/13%XReturns 7:06:05%%'%' character literally. %
Example 3: Common format codes: Returns date and time:% c,% x,% X
from datetime import datetime timestamp = 1562733155 date_time = datetime.fromtimestamp(timestamp) d = date_time.strftime("%c") print("Output 1:", d) d = date_time.strftime("%x") print("Output 2:", d) d = date_time.strftime("%X") print("Output 3:", d)
Results returned:
Output 1: Wed Jul 10 11:32:35 2019 Output 2: 07/10/19 Output 3: 11:32:35
Previous post: Datetime in Python
Next lesson: strptime () function in Python
FAQ
What is Handle Time in Python: Strftime ()() Function in Python about?
It provides a structured overview of handle time in python, explains the main context, and highlights practical takeaways for readers.
Why does this topic matter?
Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.
How should readers use this information?
Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.
Reader Comments 0
Sign in with email or Google to join the discussion.