Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse and format the date from the GitHub API in Python [duplicate]

The current value that is returned from a GitHub API request looks like this:

2013-09-12T22:42:02Z

How do I parse this value and make it look nicer?

like image 522
IQAndreas Avatar asked Sep 13 '13 21:09

IQAndreas


1 Answers

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

To convert that string into a Python date object, use the module datetime:

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

You can then parse this string to the format of your choosing using date.strftime():

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

Or if you want it to be more "automatic", the directive %c will automatically choose a date/time string based on your system's locale 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 customize it, a full list of directives can be found here: http://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

like image 78
IQAndreas Avatar answered Sep 22 '22 02:09

IQAndreas