Parsing and formatting dates from the GitHub API in Python

The current value returned by the GitHub API request is as follows:

2013-09-12T22:42:02Z

How to parse this value and make it better?

+4
source share
1 answer

The date format returned by the GitHub API is in the format ISO 8601: YYYY-MM-DDTHH:MM:SSZ

To convert this string to a Python date object, use the datetime module:

 import datetime date = datetime.datetime.strptime(<date_string>, "%Y-%m-%dT%H:%M:%SZ") 

Then you can parse this string in your chosen format using date.strftime() :

 # Result: Thursday Sep 12, 2013 at 22:42 GMT date.strftime('%A %b %d, %Y at %H:%M GMT') 

If you want it to be more β€œautomatic,” the %c directive will automatically select a date / time string based on your system’s language settings and language settings.

 # On my system, I get the following output: # Thu Sep 12 22:42:02 2013 date.strftime('%c') 

If you want to configure it, a complete list of directives can be found here: http://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

+7
source

Source: https://habr.com/ru/post/1502126/


All Articles