Created
March 12, 2019 23:47
-
-
Save hamletbatista/bb05b03b96756826b3c5888c418fc7ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Reformating date strings | |
#Crawled and first discovered dates from the Search Console API: webmasters.urlcrawlerrorssamples.list | |
last_crawled= "2019-01-12T04:00:59.000Z" #ISO-8601 date | |
first_detected= "2018-11-19T02:59:25.000Z" | |
from datetime import datetime | |
#Here is how to parse dates the hard way. See https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior | |
dt = datetime.strptime(last_crawled, '%Y-%m-%dT%H:%M:%S.%fZ') #you need to create a precise date format string | |
print(dt) | |
#output -> datetime.datetime(2019, 1, 12, 4, 0, 59) | |
#Here is how to do this the easy way using https://pypi.org/project/python-dateutil/ | |
import dateutil.parser | |
dt = dateutil.parser.parse(last_crawled) | |
print(dt) | |
#output -> datetime.datetime(2019, 1, 12, 4, 0, 59, tzinfo=tzlocal()) | |
#Google Analytics API ga:date | |
ga_date= "20190311" | |
dt = dateutil.parser.parse(ga_date) | |
print(dt) | |
#output -> datetime.datetime(2019, 3, 11, 0, 0) | |
#Latest links crawled date | |
Last_crawled= "Mar 4, 2019" | |
dt = dateutil.parser.parse(Last_crawled) | |
print(dt) | |
#output -> datetime.datetime(2019, 3, 4, 0, 0) | |
#finally we can format any these datetime objects using a consistent format | |
print(dt.isoformat()) | |
#output -> '2019-01-12T04:00:59+00:00' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment