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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With