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.
Related
I have a string from a pdf that I want to transform it to the date format that I want to work with later,
the string is
05Dec22
how can I change it to 12/05/2022?
import datetime
date1 = '05Dec22'
date1 = datetime.datetime.strptime(date1, '%d%m%Y').strftime('%m/%d/%y')
date1 = str(date1)
This is what i tried so far
If you execute the code you'll get the following error,
ValueError: time data '05Dec22' does not match format '%d%m%Y'
this is because your time string is not in the specified format given ('%d%m%Y'). You can search for tables on the internet which show the placeholders that represent a certain formatting, if you look at the one provided here, you'll see that the formatting your string has is '%d%b%y', in this case, the %b placeholder represents the abbreviated month name and the %y placeholder is the year without century, just as your example string. Now, if you fix that in your code,
import datetime
date1 = '05Dec22'
date1 = datetime.datetime.strptime(date1, '%d%b%y').strftime('%m/%d/%Y')
date1 = str(date1)
you'll get the desired result.
Note that you also have to change the output format in strftime. As I said before, the %y placeholder is the year without century. For you to get the year including the century, you have to use %Y.
How to return values only within a specific date range?
I am new to python
My code is:
for report_date in REPORT_DATE_TYPES:
if report_date in result:
date = result[report_date].split(' ')[0]
date = datetime.strptime(date, '%d/%m/%y ')
but I am getting an error:
raise ValueError("time data %r does not match format %r" %
ValueError: time data '02/03/2022' does not match format '%d/%m/%y '
How to fix this?
To point out the year you need to use %Y and also there is an additional space at the end of the format you gave that it's not present in the date. Try with date = datetime.strptime(date, '%d/%m/%Y')
The %y refers to just the last 2 digit of the year, not the whole year as you have in the example.
20/03/21 is in format '%d/%m/%y while
20/03/2021 is in format '%d/%m/%Y(note the capital Y) therefore you just need to update the code as following
for report_date in REPORT_DATE_TYPES:
if report_date in result:
date = result[report_date].split(' ')[0]
date = datetime.strptime(date, '%d/%m/%Y ')
You can find an useful table of each flag on this link
This error indicates that format and input are not aligned.
In this example the input: '02/03/2022',
does not match format '%d/%m/%y'
Why?
Since %y is a year with two letter, such as '22' rather than '2022'.
BTW, the rest of the input does match the format.
Use this link to verify the format in correct:
https://www.geeksforgeeks.org/python-datetime-strptime-function/
I am attempting to create a time series index using pandas. Currently this is the code I am running:
date_string = df3["Date"]
date_times = pd.to_datetime(date_string, yearfirst=True, format='%Y%m%d%H%M')
df3_i = df3.set_index(date_times)
Yet I am getting constant errors. Can anyone explain?
Error:
ValueError: time data '2017-03-08 13:53' does not match format '%Y%m%d%H:%M' (match)
That's because the format is '%Y-%m-%d %H:%M'
There are special character combinations that are meant to represent the numeric components of the date and time. A great reference can be found here
You have a time string of '2017-03-08 13:53' as evidenced by you error message. From the link you'll find that:
4 digit year is '%Y'
2 digit month is '%m'
2 digit day is '%d'
2 digit hour is '%H'
2 digit minute is '%M'
So you still need to represent the other string bits like the dashes, space, and the colon
Thus '%Y-%m-%d %H:%M'
Use this instead
date_string = df3["Date"]
date_times = pd.to_datetime(date_string, yearfirst=True, format='%Y-%m-%d %H:%M')
df3_i = df3.set_index(date_times)
If that doesn't work, then you have inconsistent date formats and my first course of action would be to yell at whoever created the thing I'm trying to parse.
If that happens to be your scenario, ask another question... Or I might.
I have a datetime type mydate in %Y-%m-%dT%H:%M:%S format.
I want to replace the hours
I did this using mydate.replace() method
Now I want to comapre it with another specific date -> myNEWdate whose format is %Y-%m-%d %H:%M:%S :
newdate = mydate.replace(hour = islot)
print newdate
appointmentDict[mydate]['time_start'] = datetime.strptime(str(newdate),"%Y/%m/%d %H:%M:%S")
The date is printed as 2015-06-26 08:00:00
and I get the error
ValueError: time data '2015-06-26 08:00:00' does not match format '%Y/%m/%d %H:%M:%S'
What should I do to resolve this
You need to set the correct format
datetime.strptime(str(newdate),"%Y-%m-%d %H:%M:%S")
To solve the exception. Although converting from datetime 2 string and backwards doesn't make much sense, as mentioned in the comments.
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