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.
Related
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 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")
I am trying to find the correct time format for this time string for the Python time module:
'2019-01-25T06:59:36.8081116Z'
I tried this '%Y-%m-%dT%H:%M:%SZ' and many variants and can't get it right.
Link to the documentation:
https://docs.python.org/2/library/time.html
This might help
import datetime
curtime = datetime.datetime.now()
curtime.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
couldn't figure out how to get 7 decimal places. Maybe your string ends with '6Z' instead of 'Z'?
I am newbie in Python and i m trying to do the following.
In PHP if we want to convert the date we use something like this:
$item_date = date("Y-m-j G:i:s", strtotime($RSSitem->pubDate));
Now, i m trying to do the same using Python, but i cant understand the exact method for doing it.
Can anyone help me by writing this line to python?
According to RSS specification pubDate must follow RFC822:
mytime.strftime("%a, %d %b %Y %H:%M:%S %z")
There are two parts:
convert pubDate from string to a datetime/time object. If format for pubDate is fixed you could use datetime.strptime() function otherwise you could use dateutil.parser.parse() or feeparser._parse_date() (the later might be easier to install).
convert datetime/time object to string using .strftime() method/time.strftime() function.
See strftime() and strptime() behavior.
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.