If this is the format:
This will be the format string for datetime: "%b %d, %Y %I:%M:%S %p %Z%z"
The method datetime.strptime will convert the string into a datetime object. The format is documented.
If the format is iso8601, it's much easier, because there is a method
Paring the ISO8601 timestamp manually is also possible, but not required.
For example you have a datetime object created, to compare it with your timestamps:
Depending on you input, you can decide what do to.
Jan 27, 2020 4:36:02 PM GMT+0000
This will be the format string for datetime: "%b %d, %Y %I:%M:%S %p %Z%z"
- %b: Month as locale’s abbreviated name.
- %d: Day of the month as a zero-padded decimal number.
- %Y: Year with century as a decimal number.
- %I: Hour (12-hour clock) as a zero-padded decimal number.
- %M: Minute as a zero-padded decimal number.
- %S: Second as a zero-padded decimal number.
- %p: Locale’s equivalent of either AM or PM.
- %Z: Time zone name (empty string if the object is naive).
- %z: UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).
The method datetime.strptime will convert the string into a datetime object. The format is documented.
If the format is iso8601, it's much easier, because there is a method
datetime.datetime.fromisoformat
which parses this format specification.from datetime import datetime as dt my_format = "%b %d, %Y %I:%M:%S %p %Z%z" my_timestamp = "Jan 27, 2020 4:36:02 PM GMT+0000" # now use strptime, which is the abbreviation for "string put time", # which is used to create a datetime object from str # datetime.datetime.strptime(your_date, your_format) # strftime means "string format time" # which is used to convert datetime to a str my_date = dt.strptime(my_timestamp, my_format)ISO8601 example:
from datetime import datetime as dt my_timestamp = "2020-01-29T14:12:53.439493+01:00" my_date = dt.fromisoformat(my_timestamp)In both cases the offset from utc is known. The resulting datetime-object has the timezone offset included.
Paring the ISO8601 timestamp manually is also possible, but not required.
from datetime import datetime as dt iso8601_fmt = '%Y-%m-%dT%H:%M:%S%z' ts = "2020-01-28T20:32:25+10:00" my_date = dt.strptime(ts, iso8601_fmt)Don't forget, that there are two different types datetime objects.
- datetime with timezone information
- naive datetime without timezone information
For example you have a datetime object created, to compare it with your timestamps:
from datetime import datetime as dt not_before = dt(2020, 1, 10) # no timezone my_timestamp = dt.fromisoformat("2020-01-28T20:32:25+10:00") if my_timestamp < not_before: print(f'{my_timestamp} is before {not_before}') else: print(f'{my_timestamp} is not before {not_before}')
Error:TypeError: can't compare offset-naive and offset-aware datetimes
To fix this problem with external dependencies:from datetime import datetime as dt # pip install pendulum import pendulum my_timezone = pendulum.timezone('Europe/Berlin') not_before = my_timezone.datetime(2020, 1, 10) my_timestamp = dt.fromisoformat("2020-01-28T20:32:25+10:00") if my_timestamp < not_before: print(f'{my_timestamp} is before {not_before}') else: print(f'{my_timestamp} is not before {not_before}')To handle timezone direct with datetime is a bit annoying.
Depending on you input, you can decide what do to.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!