This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
How can I parse '2020-07-30T20:40:33.1000000Z' using datetime.strptime
(1 answer)
Closed 17 days ago.
This post was edited and submitted for review 17 days ago.
An API is providing a time stamp in the following format
s = "2023-02-02T21:05:07.2207121Z"`
I'm attempting to convert it to a datetime object
dt = datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%fZ")
It's causing the following error
line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data '2023-02-02T21:05:07.2207121Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
This question was marked as a duplicated linked to a post suggesting fromisoformat(s), but that fails as well
dt = datetime.fromisoformat(s)
error
dt = datetime.fromisoformat(s)
ValueError: Invalid isoformat string: '2023-02-02T21:05:07.2207121Z'
Related
This question already has answers here:
time data does not match format
(11 answers)
ValueError: time data does not match format '%Y-%m-%d %H:%M:%S.%f'
(3 answers)
Closed 26 days ago.
ValueError: time data '2023-01-24 15:31:18.157169' does not match format '%y-%m-%d %H:%M:%S.%f'
what is the reason for this error
This question already has answers here:
Convert string into datetime.time object
(4 answers)
Closed 1 year ago.
I have a string in the str format
PATTERN_OUT = "%H:%M"
date_time = (datetime.strftime(enddateandtime, PATTERN_OUT))
I need to convert it to datetime.time. How can this be done?
You can utilize the fact that time string is \d\d:\d\d.
Look at the following snippet.
from datetime import time
time_str = "10:01"
time(*map(int, time_str.split(':')))
Add exception handler if required.
This question already has answers here:
Convert timestamps with offset to datetime obj using strptime
(4 answers)
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 2 years ago.
I have a datetime string I get from a database and I want to convert it to unix timestamp.
I am not sure what is the way to do it.
db_timestamp = '2020-08-05 12:48:50+02:00'
f = '%Y-%m-%d %H:%M:%S%z'
timestamp = datetime.strptime(db_timestamp , f)
TypeError: strptime() argument 1 must be str, not datetime.datetime
Another way I tried was as following
python_timestamp = datetime.isoformat(db_timestamp)
test_timestamp = datetime.strptime(python_timestamp , f)
Then I get the following error
ValueError: time data '2020-08-05T12:48:50+02:00' does not match format '%Y-%m-%d %H:%M:%S%z'
How to fix this error?
What should be the correct string format for db_timestamp?
assuming you run Python 3.7 or higher, what you want is fromisoformat to parse the string and timestamp() to get seconds since the epoch UNIX time (POSIX).
from datetime import datetime
db_timestamp = '2020-08-05 12:48:50+02:00'
# to datetime object:
dt = datetime.fromisoformat(db_timestamp)
# to UNIX time:
ts = dt.timestamp()
print(repr(dt), ts)
>>> datetime.datetime(2020, 8, 5, 12, 48, 50, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))) 1596624530.0
This question already has answers here:
Detecting date format and converting them to MM-DD-YYYY using Python3
(3 answers)
Closed 3 years ago.
I have a python code for time.strptime and the format is like this:
start_date = time.strptime('2019-03-25', '%Y-%m-%d')
But now if my users try to input the time that is not according to the format it will give an error.
Let's say if users try to input the date format like this:
start_date = time.strptime('25-03-2019', '%Y-%m-%d')
That will give an error message
ValueError: time data '25-03-2019' does not match format '%Y-%m-%d'
So, how to encode the string to automatically give to the format the server provide..?
While there will always be cases that are ambiguous, you can use dateutil (python-dateutil on pypi) to parse dates from most standard formats:
>>> import dateutil
>>> dateutil.parser.parse('25-03-2019')
datetime.datetime(2019, 3, 25, 0, 0)
# ^ ^ ^
# year, month, day
This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 5 years ago.
With the time value being:
value = '2017-08-31T02:24:29.000Z'
I try to convert it to a datetime object with:
import datetime
datetime_object = datetime.datetime.strptime(value, '%Y-%b-%d %I:%M%p')
But the command crashes with the exception:
ValueError: time data '2017-08-31T02:24:29.000Z' does not match format '%Y-%b-%d %I:%M%p'
You should be using a builtin Python's datautil module instead of date time:
import dateutil.parser
value = '2017-08-31T02:24:29.000Z'
result = dateutil.parser.parse(value)
First of all, you are missing the formatter for the microsecond.
Second of all, there is no second colon for dividing the minute and second.
Third, the %b operator is for the monthname (Jan,Feb,etc.). You want to use %m.
Final format is '%Y-%m-%dT%I:%M:%S.%fZ'.
This is your code:
datetime_object = datetime.datetime.strptime(value, '%Y-%m-%dT%I:%M:%S.%fZ')
You should get 2017-08-31 02:24:29 as the value of datetime_object.