Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime strptime in python

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'am 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'am using python 2.7

Any help would be appreciated.

like image 343
VorX Avatar asked Jun 16 '17 18:06

VorX


People also ask

What does datetime Strptime do in Python?

Python time strptime() function The strptime() function in Python is used to format and return a string representation of date and time. It takes in the date, time, or both as an input, and parses it according to the directives given to it.

What does Strptime mean in Python?

Description. Python time method strptime() parses a string representing a time according to a format. The return value is a struct_time as returned by gmtime() or localtime().

What is Strptime and Strftime in Python?

strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.

How do I format a datetime object in Python?

Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.


5 Answers

You need to make sure you provide input accordingly

datetime.strptime(date_string,date_string_format).strftime(convert_to_date_string_format)

To print the date in specified format you need to provide format as below.

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

Output:

01/27/2012

>>Demo<<

like image 158
Ravi Avatar answered Sep 19 '22 13:09

Ravi


datetime.strptime(date_string, format) function returns a datetime object corresponding to date_string, parsed according to format. When you print datetime object, it is formatted as a string in ISO 8601 format, YYYY-MM-DDTHH:MM:SS

References:

  • https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime
  • https://docs.python.org/2/library/datetime.html#datetime.datetime.isoformat
like image 21
taras Avatar answered Sep 17 '22 13:09

taras


As astutely noted in the comments, you are parsing to a datetime object using the format you specified.

strptime(...) is String Parse Time. You have specified the format for how the string should be interpreted to initialize a Datetime object, but that format is only utilized for initialization. By default, when you go to print that datetime object, you are getting the representation of str(DatetimeObjectInstance) (in your case, str(d)).

If you want a different format, you should use String Format Time (strftime(...))

like image 35
Spencer D Avatar answered Sep 19 '22 13:09

Spencer D


import datetime

str_time= "2018-06-03 08:00:00"
date_date = datetime.datetime.strptime(str_time, "%Y-%m-%d %H:%M:%S")
print date_date
like image 32
Mahmudul Hasan Avatar answered Sep 20 '22 13:09

Mahmudul Hasan


There is a difference between datetime.strp[arse]time() and datetime.strf[ormat]time().

The first one, strptime() allows you to create a date object from a string source, provided you can tell it what format to expect:

strDate = "11-Apr-2019_09:15:42"
dateObj = datetime.strptime(strDate, "%d-%b-%Y_%H:%M%S")

print(dateObj)  # shows: 2019-04-11 09:15:42

The second one, strftime() allows you to export your date object to a string in the format of your choosing:

dateObj = datetime(2019, 4, 11, 9, 19, 25)
strDate = dateObj.strftime("%m/%d/%y %H:%M:%S")

print(strDate)  # shows: 04/11/19 09:19:25

What you're seeing is simply the default string format of a datetime object because you didn't explicitly tell it what format to use.

Checkout http://strftime.org/ for a list of all the different string format options that are availble.

like image 38
RedKoder Avatar answered Sep 19 '22 13:09

RedKoder