I am trying to convert a string with "2019-01-06T01:00:24.908821" to a date using the "datetime.strptime" function. However, I am not able to find the format for this conversion to be successful.
I'm using the entries as proposed by the library itself, however I'm getting a "ValueError" when I try to perform the conversion.
ValueError: time data '2019-01-06T01:00:24.908821' does not match format '%Y-%m-%dT%H:%M:%S:%f'
If you would like to read the proposed standards, you can find here:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
My code:
from datetime import datetime
datetime.strptime("2019-01-06T01:00:24.908821", "%Y-%m-%dT%H:%M:%S:%f")
You have a colon (:) instead of a decimal (.) before the %f in your format string.
Change
datetime.strptime("2019-01-06T01:00:24.908821", "%Y-%m-%dT%H:%M:%S:%f")
To
datetime.strptime("2019-01-06T01:00:24.908821", "%Y-%m-%dT%H:%M:%S.%f")
Related
I want to convert a string in the following format to a datetime object in Python.
'2019-11-08T13:22:19.173700864-05:00'
I tried to use:
obj = datetime.strptime('2019-11-08T13:22:19.173700864-05:00', '%Y-%m-%dT%H:%M:%S.%f000%z')
I got an error message. It seems %f000 is not the correct way to convert 9-digit after the decimal point. And it seems I am also using %z in the wrong way.
What is the correct way to do this? Thanks.
So i try to convert my string to date time object without knowing the format this way:
date = '019-03-13 17:35:35.855'
date_object = datetime.fromisoformat(date)
So this works fine but in case the datetime object failed if the format is with comma this fail:
date = '019-03-13 17:35:35,855'
date_object = datetime.fromisoformat(date)
ValueError: Invalid isoformat string: '2019-03-13 17:35:35,855'
And most of my files written with this comma format.
Any suggestions ?
datetime.fromisoformat expects you to send a string in a particular format:
Specifically, this function supports strings in the format(s) YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]], where * can match any single character.
If your format differs, you should use strptime and set your format in it. If you have no single format or you have dirty data, the only way you can to process it is to clean it first with some kind of data refining function.
I'm trying to convert a time string into a datetime object with strptime. The problem is that I'm getting a format error from string to datetime object.
I don't understand why this format is not appropriate for my data.
import numpy as np
from datetime import datetime
Vent_date = np.array([b'"2018-06-28 15:00:00"', b'"2018-06-28 15:00:00"'], dtype='|S21')
dates = []
for line in Vent_date:
line1 = line.decode('utf-8')
dates.append(datetime.strptime(line1,'%Y-%m-%d %H:%M:%S'))
I get:
ValueError: time data '"2018-06-28 15:00:00"' does not match format '%Y-%m-%d %H:%M:%S'
If you notice the error contains double quotes wrapped in single quotes. So it looks like your source data has double quotes in it which is why it is failing.
A few simple solutions:
Fix (remove) the quotes in your source data and use your original code
Strip the quotes from the string before trying to parse it:
dates.append(datetime.strptime(line1.strip('"'),"%Y-%m-%d %H:%M:%S"))
Change your date format string to look for a date containing double quotes:
dates.append(datetime.strptime(line1,'"%Y-%m-%d %H:%M:%S"'))
Use Python's csv library which may handle reading csv files better
I am converting the datetime into time. My JSON datetime format is "2017-01-02T19:00:07.9181202Z". I have placed my code below:
from datetime import datetime
date_format = datetime.strptime('2017-01-02T19:00:07.9181202Z', '%Y-%m-%dT%H:%M:%S.%fZ')
time = date_format.strftime("%I:%M %p")
print(time)
Error message as below:
After that I read this python date-time document. It says that microsecond digit should be 6. But, JSON date-time microsecond has 7 digit.
Message from Python document:
%f is an extension to the set of format characters in the C standard
(but implemented separately in datetime objects, and therefore always
available). When used with the strptime() method, the %f directive
accepts from one to six digits and zero pads on the right.
I need result like 07:00 PM format. Is there any alternative method?
Thanks in advance.
If you're sure that the input will always be like that, you can just remove the extra digit before passing that string to strptime:
date_format = datetime.strptime('2017-01-02T19:00:07.9181202Z'[:-2] + 'Z', '%Y-%m-%dT%H:%M:%S.%fZ')
This is dirty, but gives the idea - remove the last two characters (the extra digit and "Z"), re-add the "Z".
I'me trying to use this eventCalendar in Django, which saves and shows dates in this format:
2012-02-27T13:15:00.000+10:00
but when I save events in the database, they're saved in this format:
Mon Feb 27 2012 13:15:00 GMT+0330 (Iraq Standard Time)
so events from the database won't appear on the calendar because of this format. How can I convert this format?
I tried some thing like this:
datetime.strptime(mydatetime, "%Y-%m-%dT%H:%M:%S+0000")
but I'm repeatedly getting errors like this:
'module' object has no attribute 'strptime'
Edit:date is in string format
strptime is used to parse a string into a datetime object. The format string indicates how to parse the string, not the format you want the datetime to take when later printed as a string. So first off you need to make the format string match the date format of the input string.
Once you've gotten a datetime from strptime, you can then use strftime with your current format string to get it into the display you want.
That said, though, it appears you've got a problem with your imports. The error seems to indicate that you've done:
import datetime
datetime.strptime(...)
That's incorrect. strptime and strftime are methods off datetime.datetime, so you need to either modify your import like:
from datetime import datetime
Or, modify your call to strptime like:
datetime.datetime.strptime(...)
UPDATE
You're starting off with a string like Mon Feb 27 2012 13:15:00 GMT+0330 (Iraq Standard Time). Python is pretty awesome, but it's not omniscient; if you want to convert this to a datetime you have to tell it how. That's the purpose of the format string you pass to strptime. You need to create a format string that represents your current string date and time as represented in the database (exercise left to reader). Think in reverse, along the lines of it you wanted to actually represent a datetime like that, how would you do it.
This will net you a datetime. From there, you can now format that datetime as a string with strftime, passing the actual format you want, this time.
So the process is:
Create a format string representing your current string from the database
Use that format string as an argument to strptime to get a datetime
Create a format string representing the format you want the date to be in (already done)
Use that format string as the argument to strftime to convert the datetime from step 2 to your desired string.