English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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.
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.
We imported the class from the datetime module. This is because the datetime class object can access the strftime() method.
The datetime object containing the current date and time is stored innowin
The strftime() method can be used to create a formatted string.
The string you pass to the strftime() method may contain multiple format codes.
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
The following table shows all the codes you can pass to the strftime() method.
Command | Meaning | For example |
%a | Abbreviation of the workday name. | Sun, Mon, ... |
%A | Full name of the workday. | Sunday, Monday, .. |
%w | Workday represented as a decimal number. | 0,1, ... ,6 |
%d | A day of the month, zero-padded as a decimal number. | 01, 02, ... ,31 |
%-d | A day of the month represented as a decimal number. | 1,2, ... ,30 |
%b | Abbreviation of the month. | Jan, Feb, ..., Dec |
%B | Complete month name. | January, February, ... |
%m | The month, zero-padded as a decimal number. | 01、02, ... ,12 |
%-m | The month represented as a decimal number. | 1,2, ... ,12 |
%y | A year without centuries, zero-padded as a decimal number. | 00, 01, ... ,99 |
%-y | A year without centuries, represented as a decimal number. | 0,1, ... ,99 |
%Y | A year represented as a decimal number with centuries. | 2013,2019and |
%H | Hour (24Hour-based), a zero-padded decimal number. | 00, 01, ... ,23 |
%-H | Hour (24(12-hour clock) as a decimal number. | 0,1, ... ,23 |
%I | Hour (12Hour-based), a zero-padded decimal number. | 01、02, ... ,12 |
%-I | Hour (12(12-hour clock) as a decimal number. | 1 2 2 |
%p | The appropriate morning or afternoon in the language environment. | AM, PM |
%M | Minute, a decimal number with leading zeros. | 00, 01, ... ,59 |
%-M | Represented by a decimal number. | 0,1, ... ,59 |
%S | The second decimal number with leading zeros. | 00, 01, ... ,59 |
%-S | The second decimal digit. | 0,1, ... ,59 |
%f | Microseconds, a decimal number, padded with zeros on the left. | 000000-999999 |
%z | The UTC offset, formatted as+ HHMM or-HHMM. | |
%Z | The time zone name. | |
%j | The day of the year, represented by a decimal number with leading zeros. | 001, 002, ... ,366 |
%-j | The day of the year, represented by a decimal number. | 1,2, ... ,366 |
%U | The 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 |
%W | The 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 |
%c | The appropriate date and time representation in the language environment. | Mon Sep 30 07: 06: 05 2019 |
%x | The appropriate date representation in the language environment. | 13/9/30 |
%X | The appropriate time representation in the language environment. | 07: 06: 05 |
%% | The character "%". | % |
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.