I have string date data with the following format:
4/16/15 23:50
When I try to convert into a datetime object:
print datetime.datetime.strptime(fecha2, '%d/%m/%y %H:%M')
I get this error:
ValueError: time data '4/16/15 23:50' does not match format '%d/%m/%y %H:%M'
According to this list:
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior, I am using the right format. Where am I wrong?
you have month and day reversed:
print datetime.datetime.strptime(fecha2, '%m/%d/%y %H:%M')
Also very handy:
from dateutil.parser import parse
parse("4/16/15 23:50")
I know this topic is old but maybe it will help someone. I have faced recently similar issue where the date was actually correctly typed and it throwed the same error:
datetime.datetime.strptime('2018-05-02T14:27:56+00:00', "%Y-%m-%dT%H:%M:%S%z")
error: time data '2018-05-02T14:27:56+00:00' does not match format '%Y-%m-%dT%H:%M:%S%z'
The issue was that i was trying to run the python 3.7 code with python 3.6
Related
I have a date as "1-Jun". How can I convert this to 01/06? I am using Strptime. I thought this is going to be easy but it is not.
Error that I am getting: time data '1-Jun' does not match format '%d-%Mmm'.
This is the command I am using. Can anyone help me with this?
datetime.datetime.strptime(date, '%d-%Mmm').strftime('%m/%d')
There's no such format as %Mmm, what you need to match Jun is %b ("Locale's abbreviated month name"). Also, if you want 01/06 rather than 06/01 it is going to be '%d/%m' in strftime:
print(datetime.datetime.strptime('1-Jun', '%d-%b').strftime('%d/%m'))
I am currently having an issue converting an incoming datetime string to a datetime object using Python's built in strptime() method. Here is what I currently have:
def fixDateTimeField(datetimeString):
# Convert from 2012-08-07T00:00:00Z to 2012-08-07T00:00:00.000Z
datetime_object = datetime.strptime(datetimeString, "%y-%m-%dT%H:%M:%SZ")
return datetime_object
Like the comment says, I am trying to convert the string "2012-08-07T00:00:00Z" to a datetime object that looks like 2012-08-07T00:00:00.000Z but I am getting an error in my console that says: "ValueError: time data '2012-08-07T00:00:00Z' does not match format '%y-%m-%dT%H:%M:%SZ'". The format seems correct to me and i'm not seeing the issue.
Thanks in advance!
%y is for two-digit years. You have a four-digit year.
Try using %Y instead.
>>> from datetime import datetime
>>> datetimeString = "2012-08-07T00:00:00Z"
>>> print(datetime.strptime(datetimeString, "%Y-%m-%dT%H:%M:%SZ"))
2012-08-07 00:00:00
A nice way to parse your iso-8601 datetime string "2012-08-07T00:00:00Z" to a datetime object is using dateutil.
import dateutil.parser
yourdate = dateutil.parser.parse(datestring)
With strptime:
datetime.datetime.strptime( "2007-03-04T21:08:12", "%Y-%m-%dT%H:%M:%S")
Works. As an other answer said, the Y must be capitalized for strptime to work with 4 digit years.
I'm using the following code:
data['Input_volTargetStart'][1]>time.strptime(data['Dates'][1], "%d %b $y")
When I try to run it, I get this error:
ValueError: time data '04-Jun-99' does not match format '%d %b $y'
I have tried possibly all combinations but am unable to get the result for this conversion.
You can actually put the dashes ('-') as part of the format, i.e "%d-%b-%y".
This is what you'll need.
from datetime import datetime
a = "04-Jun-99"
frmt = datetime.strptime(a, "%d-%b-%y")
print(frmt)
>>1999-06-04 00:00:00
Implement that example into your code to get proper output.
I am somewhat new to Python and have a seemingly simple question.
I have a python script that interacts with an API (RHN Satellite if you're curious). This API returns a date in the form of a string and it always trims leading 0's. For example, 6/1/13 or 10/9/12. I need to convert this string to a date and determine the day of the year it is.
Here is what I know:
today = datetime.datetime.now()
print today.strftime('%j')
...will return today's day of year (175). This works fine for a datetime object but I am having trouble converting the string given by the API to an actual date. If I use:
date = datetime.datetime.strptime(var, '%m/%d/$y')
I get error:
ValueError: time data '5/2/13' does not match format '%m/%d/$y'
I'm guessing because it's expecting leading 0's ? How do I get around this?
In the end, I am trying to subtract the variable date given from the current date but I can't do that until I convert the string.
Thanks for the help!
I think you just have a typo, use %y instead of $y:
date = datetime.datetime.strptime(var, '%m/%d/%y')
This code works for me, provided you change $y to %y in the format code.
Correct the $y to %y and I'd use format instead of strftime:
from datetime import datetime
print format(datetime.strptime('5/2/13', '%m/%d/%y'), '%j')
a have a litle problem to parse a string into a datetime object in python.
The following code working for some values but not always and a dont know whats wrong with this litel peace of code.
datetime.datetime.strptime("22.12.2012 17:00", '%d.%m.%Y %I:%M')
ValueError: time data '22.12.2012 17:00' does not match format '%d.%m.%Y %I:%M'
I think the problem clould somthing with the time and am/pm?. Because the error pops only by time > 12:00 and the string "22.12.2012 17:00" works fine.
Thanks for help
Try the %H token instead of %I:
import datetime
datetime.datetime.strptime("22.12.2012 17:00", '%d.%m.%Y %H:%M')
%I is for 12-hour date format. %H is for 24-hour date format, as explained in the docs: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior