datetime.strptime () throws 'does not match format' error - python

I get
time data '19/Apr/2011:22:12:39' does not match format '%d/%b/%y:%H:%M:%S'
when using datetime.strptime('19/Apr/2011:22:12:39','%d/%b/%y:%H:%M:%S')
What am I doing wrong?

Try %d/%b/%Y:%H:%M:%S instead - %y right now means 11.
You can "debug" datetime formats easily using date (on the shell and not on python, I mean, assuming you're running GNU/Linux or similar):
date '+%d/%b/%Y:%H:%M:%S'
05/May/2011:09:00:41

You're checking for a 2 digit year ( %y ) instead of a four digit ( %Y )

You want %Y instead of %y. %Y means you want the century, %y is no century and the year is displayed from 00 to 99.

Related

Wrong datetime information

I have the string "5-11-2019" in DAY/MONTH/YEAR format, and am doing the following to increment 1 day:
datetime.datetime.strptime(str("5-11-2019"), '%d-%M-%Y') + datetime.timedelta(days=1)
However, instead of getting the result of 2019-11-06, Python returns 2019-01-06, removing 10 months, which I cannot for the life of me understand.
You used %d-%M-%Y as format, but %M stands for minute, not month. You should use %d-%m-%Y (lower m).
You should be using %m instead of %M
%m = Months
%M = Minutes
Check the “%M”.
Also keep in mind that
%d = Day of the month as a zero-padded decimal number.
and
%-d = Day of the month as a decimal number. (Platform specific)
[From https://strftime.org/]
So either pad your days with 0 or use “%-d”.

Time data does not match format - ValueError

I am trying to change string to datetime like below:
max_datetime = datetime.strptime(max_date,'%y-%m-%d %H:%M:%S')
However, I am getting the below-mentioned error:
ValueError: time data '2008-05-15 11:26:40' does not match format '%y-%m-%d %H:%M:%S'
Any help will be appreciated!
The documentation of datetime tells that %y (with a lowercase y) represents a two-digit year, while from the error-message we can see that your input, max_date has a four-digit year. A four-digit year is represented by %Y (with an uppercase Y). So this is the source of your error. Since the rest looks fine,
max_datetime = datetime.strptime(max_date, "%Y-%m-%d %H:%M:%S")
should do the job.

converting date string to timestamp

I have date strings like '1997-05-07 ' ('year-month-day') and when I try to convert it to timestamp (in order to compare them and have some plots I need to convert them ) I get this error :
ValueError: time data '1997-07-05' does not match format '%y-%m-%d'
what I try is :
time.mktime(datetime.datetime.strptime('1997-07-05','%y-%m-%d').timetuple())
The %y format expects the year without century as a decimal padded number. If you want to parse 1997, you need to use %Y.
You can view more information on how strptime works in the official documentation.
You have your format wrong - %y matches only 2-digit year. If you want "full" year, you have to use %Y:
time.mktime(datetime.datetime.strptime('1997-07-05','%Y-%m-%d').timetuple())
868053600.0

OpenERP 6.1 datetime formatting

Why does
o.create_order.strftime("%d %B %Y")
returns nothing when
time.strftime("%d %B %Y")
returns the date "10 february 2013"???
o.create_order is a timestamp according to postgresql.
It contains "30/11/2012 09:38:34" as seen on the openErp sale order - Other information tab.
It is stored as "2012-11-30 08:38:34.272" when querying the database.
So I would expect to see "30 November 2012" but get nothing instead.
Am I misinterpreting the syntax?
I tested this from python 3.3:
>>> d1=datetime.datetime.today()
>>> print(d1.strftime("%d %B %Y"))
10 february 2013
How do I get it to work in OpenOffice Writer?
And by the way how do I get "February" instead of "february"?
Because o.create_order returns a string and not a datetime object, even if, internally, the database column is a timestamp. The OpenERP ORM returns a string in ISO 8601 format.
You need to use the formatLang method which is available in RML reports or create a datetime object using the datetime python module.
Try this:
datetime.strftime('%d %B %Y', o.create_order')
It is because o.create_order returns a string. So first you have to convert your string date into datetime format and then again you can convert it into any format you want as a string.
Try this:
#Converts string into datetime format.
dt1 = datetime.strptime(o.create_order,"%Y-%m-%d %H:%M:%S")
#Converts datetime into your given formate and returns string.
dt2 = datetime.strftime(dt,"%d %B %Y")
Hope this will solve your problem.

String to datetime

I saved a datetime.datetime.now() as a string.
Now I have a string value, i.e.
2010-10-08 14:26:01.220000
How can I convert this string to
Oct 8th 2010
?
Thanks
from datetime import datetime
datetime.strptime('2010-10-08 14:26:01.220000'[:-7],
'%Y-%m-%d %H:%M:%S').strftime('%b %d %Y')
You don't need to create an intermediate string.
You can go directly from a datetime to a string with strftime():
>>> datetime.now().strftime('%b %d %Y')
'Oct 08 2010'
There's no one-liner way, because of your apparent requirement of the grammatical ordinal.
It appears you're using a 2.6 release of Python, or perhaps later. In such a case,
datetime.datetime.strptime("2010-10-08 14:26:01.220000", "%Y-%m-%d %H:%M:%S.%f").strftime("%b %d %Y")
comes close, yielding
Oct 08 2010
To insist on '8th' rather than '08' involves calculating the %b and %Y parts as above, and writing a decimal-to-ordinal function to intercalate between them.

Categories