Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23218

John Ludhi/nbshare.io: Strftime and Strptime In Python

$
0
0

Strftime and Strptime In Python

In this post, we will learn about strftime() and strptime() methods from Python datetime package.

Python Strftime Format

The strftime converts date object to a string date.

The syntax of strftime() method is...

dateobject.strftime(format)

Where format is the desired format of date string that user wants. Format is built using codes shown in the table below...

CodeMeaning
%aWeekday as Sun, Mon
%AWeekday as full name as Sunday, Monday
%wWeekday as decimal no as 0,1,2...
%dDay of month as 01,02
%bMonths as Jan, Feb
%BMonths as January, February
%mMonths as 01,02
%yYear without century as 11,12,13
%YYear with century 2011,2012
%H24 Hours clock from 00 to 23
%I12 Hours clock from 01 to 12
%pAM, PM
%MMinutes from 00 to 59
%SSeconds from 00 to 59
%fMicroseconds 6 decimal numbers

Datetime To String Python using strftime()

Example: Convert current time to date string...

In [8]:
importdatetimefromdatetimeimportdatetimenow=datetime.now()print(now)
2021-03-07 23:24:11.192196

Let us convert the above datetime object to datetime string now.

In [2]:
now.strftime("%Y-%m-%d %H:%M:%S")
Out[2]:
'2021-03-07 23:16:41'

If you want to print month as locale’s abbreviated name, replace %m with %b as shown below...

In [3]:
now.strftime("%Y-%b-%d %H:%M:%S")
Out[3]:
'2021-Mar-07 23:16:41'

Another example...

In [4]:
now.strftime("%Y/%b/%A %H:%M:%S")
Out[4]:
'2021/Mar/Sunday 23:16:41'

Date To String Python using strftime()

Date to string is quite similar to datetime to string Python conversion.

Example: Convert current date object to Python date string.

In [5]:
today=datetime.today()print(today)
2021-03-07 23:22:15.341074

Let us convert the above date object to Python date string using strftime().

In [6]:
today.strftime("%Y-%m-%d %H:%M:%S")
Out[6]:
'2021-03-07 23:22:15'

Python Strftime Milliseconds

To get a date string with milliseconds, use %f format code at the end as shown below...

In [7]:
today=datetime.today()today.strftime("%Y-%m-%d %H:%M:%S.%f")
Out[7]:
'2021-03-07 23:23:50.851344'

Python Strptime Format

Strptime python is used to convert string to datetime object.

strptime(date_string, format)

example:

strptime("9/23/20", "%d/%m/%y")

Note - format "%d/%m/%y" represents the the corresponding "9/23/20" format. The output of the above command will be a Python datetime object.

The format is constructed using pre-defined codes. There are many codes to choose from. The most important ones are listed below.

CodeMeaning
%aWeekday as Sun, Mon
%AWeekday as full name as Sunday, Monday
%wWeekday as decimal no as 0,1,2...
%dDay of month as 01,02
%bMonths as Jan, Feb
%BMonths as January, February
%mMonths as 01,02
%yYear without century as 11,12,13
%YYear with century 2011,2012
%H24 Hours clock from 00 to 23
%I12 Hours clock from 01 to 12
%pAM, PM
%MMinutes from 00 to 59
%SSeconds from 00 to 59
%fMicroseconds 6 decimal numbers

Python Datetime Strptime

Example: Convert date string to Python datetime object.

In [9]:
importdatetimedatetime.datetime.strptime("09/23/2030 8:28","%m/%d/%Y %H:%M")
Out[9]:
datetime.datetime(2030, 9, 23, 8, 28)

Viewing all articles
Browse latest Browse all 23218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>