How to Convert Datetime to String in Python

To convert a datetime tostring in Python, you can use the “strftime()” function. The “strftime()” function takes a format string as an argument and returns a formatted string representation of the datetime object.

Syntax

datetime.strftime(Format_String)

How to Use strftime() Method

from datetime import datetime

# current date and time
curDT = datetime.now()

# current day
day = curDT.strftime("%d")
print("day:", day)

# current month
month = curDT.strftime("%m")
print("month:", month)

# current year
year = curDT.strftime("%Y")
print("year:", year)

# current time
time = curDT.strftime("%H:%M:%S")
print("time:", time)

# current date and time
date_time = curDT.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:", date_time)

Output

day: 21
month: 01
year: 2020
time: 13:31:14
date and time: 01/21/2020, 13:31:14

How to convert Date to String in Python

You can convert a date to a string using the “strftime()” method of the datetime module. It allows you to specify a string format for the output.

# app.py

from datetime import datetime

# current date and time
curDTObj = datetime.now()

# current date
dateStr = curDTObj.strftime("%d %b, %Y")
print("dateStr:", date)

Output

date: 21 Jan, 2020

How to convert time to string in Python

Use the strftime() class method from the datetime module to convert a time to a string in Python. The strftime method allows you to specify a string format for the output.

from datetime import datetime

# current date and time
curDTObj = datetime.now()

# current time
timeStr = curDTObj.strftime("%H:%M:%S.%f")
print("Time:", timeStr)

Output

Time: 14:30:27.650890

How to convert datetime to text in Python

To convert a datetime to text in Python, you can use the strftime() function.

from datetime import datetime

# current date and time
curDTObj = datetime.now()

# current datetime
datetimeStr = curDTObj.strftime("Today is %d %B of %Y and the week day is %A at %I:%M %p")
print("Readable Text of Current Date Time:", datetimeStr)

Output

Readable Text of Current Date Time: Today is 21 January of 2020 
and the week day is Tuesday at 02:35 PM

List of the date-time format in Python

Format Codes Description Example
%d Day of the month as a zero-padded decimal number 01, 02, 03, 04 …, 31
%a Weekday as an abbreviated name Sun, Mon, …, Sat
%A Weekday as the full name Sunday, Monday, …, and Saturday
%m The month is the zero-padded decimal number 01, 02, 03, 04, 05,…, 12
%b Month as an abbreviated name Jan, Feb, Mar,…, Dec
%B Month as full name January, February, …, December
%y A year without a century as the zero-padded decimal number 00, 01, 02, 03, …, 99
%Y The year with century as the decimal number 0001, …, 2018, …, 9999
%H Hour (24-hour clock) as the zero-padded decimal number 01, 02, 03, 04, 05 …, 23
%M Minute is a zero-padded decimal number 01, 02, 03, 04, 05 …, 59
%S Second as a zero-padded decimal number 01, 02, 03, 04, 05 …, 59
%f A microsecond is a decimal number, zero-padded on the left 000000, 000001, …, 999999
%I Hour (12-hour clock) as a zero-padded decimal number 01, 02, 03, 04, 05 …, 12
%p Locale’s equivalent of either AM or PM AM, PM
%j Day of the year as a zero-padded decimal number 01, 02, 03, 04, 05 …, 366

That’s it.

Related posts

Python string to datetime

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.