I have the following format for my dates
1952-02-04 00:00:00
I need the format to be month/day/year
How do I go about this currently I have
if clientDob is not None:
clientDob = datetime.datetime.strptime(clientDob, '%Y-%m-%d').strftime('%m/%d/%y')
the error I get is
time data 'Birth Dt' does not match format '%Y-%m-%d'
Use
clientDob = datetime.datetime.strptime(clientDob, '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%y')
for datetime parsing your string must match the source string exactly. It contains time-information, you need to match it.
import datetime
clientDob = "2018-12-21 12:13:14"
clientDob = datetime.datetime.strptime(clientDob, '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%y')
print(clientDob)
Output:
12/21/18
Details: strftime-and-strptime-behavior
Related
I am trying to convert a string to datetime object using the strptime function.
I am encountering a ValueError that says format doesn't match, so I did double checking and confirmed that the format in the string matches the format I am passing as the parameter for strptime.
I have also referenced this question: time data does not match format but there the month and year were swapped.
So does this only work with the '%y-%m-%d %H:%M:%S' format or is it dynamic as per the user input like in my case '%y-%m-%d-%H:%M:%S' ?
input:-
from datetime import datetime
stg = "2022-10-31-01:17:46"
do = datetime.strptime(stg, '%y-%m-%d-%H:%M:%S')
output
ValueError: time data '2022-09-31-01:17:46' does not match format '%y-%m-%d-%H:%M:%S'
Expected output:
#while printing 'do'
2020-09-31-01:17:46
You're almost there. You need %Y instead of %y since you're providing the year with the century (2022 instead of 22).
Your code would be
from datetime import datetime
stg = "2022-10-31-01:17:46"
do = datetime.strptime(stg, '%Y-%m-%d-%H:%M:%S')
I have the following string 20211208_104755, representing date_time format. I want to convert it to the python datetime format using datetime.strip() method.
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%y/%m/%d')
However I am getting the following error.
ValueError: time data '20211208' does not match format '%y/%m/%d'
The second argument in strptime has to match the pattern of your datetime string. You can find the patterns and their meaning on https://docs.python.org/3/library/datetime.html
In your case, you can format it as
from datetime import datetime
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%Y%m%d_%H%M%S')
print(datetime_object)
>>> 2021-12-08 10:47:55
what should work is to define how the mydatetime string is composed.
example:
%Y is the year (4 digits); check here for format (section strftime() Date Format Codes)
So in your example I would assume it's like this:
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%Y%m%d_%H%M%S')
print (datetime_object)
result
2021-12-08 10:47:55
and
type(datetime_object)
datetime.datetime
So I have a list of dates that I turned into a string called dates_2. I now want to define these strings into "dates" using datetime.strptime so that I then can use datetime.strftime to format them.
the dates that are within dates_2 are these:
26/09/2021 04/12/2021 13/02/2022 11/11/2021 13/12/2022
import datetime as dt
from datetime import datetime
#Dates
dates = re.findall(r"[0-9]+/[0-9]+/[0-9]+", txt)
dates_2 = ""
for x in dates:
dates_2 += ' '+ x
dates_3 = datetime.strptime(dates_2, '%d/%m/%Y')
dates_sorted = datetime.strftime('%Y/%m/%d')
print("Dates:", dates_sorted)
The errors that I get are these:
raise ValueError("time data %r does not match format %r" %
ValueError: time data ' 26/09/2021' does not match format '%d/%m/%Y'
Any help would be really appreciated!
You need to remove the whitespace:
ValueError: time data ' 26/09/2021' does not match format '%d/%m/%Y'
The pattern does not match your string, because the string starts with a whitespace. Call .strip() on your string before passing it to the datetime functions.
I have a DataFrame with one columns that is a date and a time and is a string.
The format of the date and time is like this: 4/27/2021 12:39
This is what I have so far to try and convert the string into a datetime:
new_list = []
for i in range(len(open_times)):
date = df.iloc[i]['Open Datetime']
good_date = date.to_datetime()
# good_date = date.topydatetime()
new_list.append(good_date)
I have used to_pydatetime() in the past however the string was in a different format.
When I run the code from above I get this error: AttributeError: 'str' object has no attribute 'to_datetime' and I get the same error when I run the commented out line except with to_pydatetime.
Any thoughts on how to resolve this error? I think that this is happening because the format of the string is different than it typically is.
You need to use datetime.strptime(date_string, format) to convert a string to datetime type
from datetime import datetime
for i in range(len(open_times)):
date = df.iloc[i]['Open Datetime']
good_date = datetime.strptime(date, '%m/%d/%Y %H:%M')
But you could use pd.to_datetime directly
df['Open Datetime'] = pd.to_datetime(df['Open Datetime'])
# Convert a column to list
new_list = df['Open Datetime'].values.tolist()
I have a list of date time strings like this.
16-Aug-2019
I want to convert the string to 2019-08-01 this date format, and I have tried on this code , but it's getting me an error.
formatd_date = datetime.strptime(formatd_date, '%y-%m-%d')
ValueError: time data 'As-of' does not match format '%y-%m-%d'
If any can help, it will be huge thank.
Convert to datetime format and then convert to string format you want to:
>>> from datetime import datetime
>>> a = "16-Aug-2019"
>>> datetime.strptime(a, "%d-%b-%Y").strftime("%Y-%m-%d")
'2019-08-16'
Documentation: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
Just fails because %y is 2-digit year. Use %Y for 4-digit year.