Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I've tried the following code :

import datetime

d = datetime.datetime.strptime("01/27/2012", "%m/%d/%Y")

print(d)

and the output is :

2012-01-27 00:00:00

I'm using Linux Mint :

test@testsrv ~/pythonvault $ date 

Fri Jun 16 21:40:57 EEST 2017

So, the question is why the output of python code returns a date in "%Y/%m/%d" ( 2012-01-27 ) instead of "%m/%d/%Y" format?

Please note that I'm using python 2.7

Any help would be appreciated.

1 Answer

0 votes
by (106k points)

If you want to print the date in the specified format you need to provide format as mentioned below.

import datetime 

d=datetime.datetime.strptime("01/27/2012","%m/%d/%Y").strftime('%m/%d/%Y') 

print d

Related questions

0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.6k points)
+2 votes
2 answers
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...