datetime.datetime.strptime says wrong format. how to solve? - python

why isn't that working?
import datetime
dt = datetime.datetime.strptime("13.11.2021 16:30", "%d.%m.%y %H:%M")
Error: "ValueError: time data '13.11.2021 16:30' does not match format '%d.%m.%y %H:%M'"
thanks!!

You should use it like this:
from datetime import datetime
dl = datetime.strptime('12.11.2021 18:54', '%d.%m.%Y %H:%M')
The missing point is your year parameter --> y into Y.
Cheers.

Related

How to parse date time string with offset?

I am trying to parse this string:
string_date = "2020-10-06T12:31:15-05:00"
dt = datetime.datetime.strptime(string_date, '%Y-%m-%d %H:%M:%S %Z')
But, it doesn't work. However, I am not sure how to properly put this into a datetime object. How would I do this?
Your:
string_date = "2020-10-06T12:31:15-05:00"
seems like ISO format for me, so I did:
import datetime
string_date = "2020-10-06T12:31:15-05:00"
dt = datetime.datetime.fromisoformat(string_date)
print(dt)
print(dt.tzinfo)
Output:
2020-10-06 12:31:15-05:00
UTC-05:00
Is above what are you looking for?

How to change a datetime format in python?

How can one make 2020/09/06 15:59:04 out of 06-09-202015u59m04s.
This is my code:
my_time = '06-09-202014u59m04s'
date_object = datetime.datetime.strptime(my_time, '%d-%m-%YT%H:%M:%S')
print(date_object)
This is the error I receive:
ValueError: time data '06-09-202014u59m04s' does not match format '%d-%m-%YT%H:%M:%S'
>>> from datetime import datetime
>>> my_time = '06-09-202014u59m04s'
>>> dt_obj = datetime.strptime(my_time,'%d-%m-%Y%Hu%Mm%Ss')
Now you need to do some format changes to get the answer as the datetime object always prints itself with : so you can do any one of the following:
Either get a new format using strftime:
>>> dt_obj.strftime('%Y/%m/%d %H:%M:%S')
'2020/09/06 14:59:04'
Or you can simply use .replace() by converting datetime object to str:
>>> str(dt_obj).replace('-','/')
'2020/09/06 14:59:04'
As your error says what you give does not match format - %d-%m-%YT%H:%M:%S - means you are expecting after year: letter T hour:minutes:seconds when in example show it is houruminutesmsecondss without T, so you should do:
import datetime
my_time = '06-09-202014u59m04s'
date_object = datetime.datetime.strptime(my_time, '%d-%m-%Y%Hu%Mm%Ss')
print(date_object)
Output:
2020-09-06 14:59:04
You need to always make sure that your desired date format should match up with your required format.
from datetime import datetime
date_object = datetime.strptime("06-09-202015u59m04s", '%d-%m-%Y%Hu%Mm%Ss')
print(date_object.strftime('%Y/%m/%d %H:%M:%S'))
Output
2020/09/06 15:59:04

I'm getting an error message regarding datetime

I'm trying to turn a date with hours into just a date.
Please help me, this is driving me insane.
This is my script:
from datetime import datetime
Date1= '2018-2-12 10:30:01'
d = datetime.strptime('Date1','%Y-%m-%d %H:%M:%S')
day_string = d.strftime('%Y-%m-%d')
and this is the error message:
ValueError: time data 'Date1' does not match format '%Y-%m-%d %H:%M:%S'
You should use the variable Date1 instead of the string 'Date1'
from datetime import datetime
Date1= '2018-2-12 10:30:01'
d = datetime.strptime(Date1,'%Y-%m-%d %H:%M:%S')
day_string = d.strftime('%Y-%m-%d')
print(day_string)
#2018-02-12

The time data does not match format

Hello I am trying to convert a string which is a = "2019-04-22 00:00" to a datetime but it does not work, I tried this :
a = datetime.datetime.strptime(a, '%Y-%m-%d %H:%M')
But I got
time data 'start_period' does not match format '%Y-%m-%d %H:%M'
I precise start_period is get by this : a = request.POST.get('start_period')
Like this it should work:
import datetime
start_period = "2019-04-22 00:00"
a = datetime.datetime.strptime(start_period, '%Y-%m-%d %H:%M')
Result:
datetime.datetime(2019, 4, 22, 0, 0)
May I suggest using dateutil module, where you do not need to provide the format string
from dateutil import parser
print(parser.parse("2019-04-22 00:00"))
#2019-04-22 00:00:00
there are multiple ways to convert string to datetime in python. it is based on the requirement of the user which function to use.
one possible solution could be as given below.
import timestring
start_period = "2019-04-22 00:00"
print(timestring.Date(start_period))

Python: Get date format as a string from datetime64[ns]

I want to determine the date format of an input datetime64[ns] value, and obtain a str.
Example:
input value = 1978-07-06
output value = '%Y-%m-%d'
I've tried this but got stuck:
import dateutil.parser
from datetime import datetime
yourdate = dateutil.parser.parse(input)
datetimeobject = datetime.strptime(yourdate,'%Y-%m-%d %H:%M:%S')
Any help on this would be appreciated.
Note: dateinfer gave me "No module named 'infer' " error
extract the date, from input using regular expression
import dateutil.parser
from datetime import datetime
input = '1978-07-06' # or - 06/07/1978
date_obj = dateutil.parser.parse(input)
date_string = date_obj.strftime('%Y-%m-%d %H:%M:%S')

Categories