I'm getting an error message regarding datetime - python

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

Related

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

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.

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

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')

How to get strftime 7 hours in the past?

I have a timestamp that is in a time zone 7 hours ahead of me. The timestamp is saved in the strftime format '%Y-%m-%d %H:%M'. How do I make this timestamp go 7 hours back?
Example: Timestamp right now: 2014-4-14 3:00. What I want: 2014-4-13 20:00
Another option:
>>> from datetime import datetime, timedelta
>>> str(datetime.strptime('2014-4-14 3:00', '%Y-%m-%d %H:%M') + \
timedelta(hours = -7))
'2014-04-13 20:00:00'
>>> datetime.strftime(datetime.now() + timedelta(hours=-7),'%Y-%m-%d %H:%M')
'2014-04-13 14:45'
I like the following if I have a datetime. Are you saying you only have string to work with?
import datetime
from dateutil.relativedelta import relativedelta
expires = datetime.datetime.now() - relativedelta(hours=7)

Categories