-3

How can I parse this date:

2016-06-10T00:00:00Z

into:

Jun 6, 2016

Using Python 2.7 without a third-party lib?

I have seen dateparser and datetime, but am not sure the best approach to take.

JohnnyP
  • 426
  • 1
  • 7
  • 12
  • 1. Why *"without a third-party lib"*. 2. Have you tried taking *any* approach? 3. How would you even measure *"best"*. – jonrsharpe Jun 21 '16 at 10:26
  • 1) because i am on a server environment that is prohibitive to installing modules-ie thet have to be compiled from source instead of PIP install – JohnnyP Jun 21 '16 at 14:44
  • 2) I have tried using strptime() but was unsuccesful – JohnnyP Jun 21 '16 at 14:46
  • 3) Searches for the term "best way" on SO return 435,273 results, it is a common expression I guess i picked up from other SO users that usually gets responses with pro/con or specific things to look out for. That is what I was expecting. – JohnnyP Jun 21 '16 at 14:50
  • How is `datetime` a "third-party lib"? Who is the third party? It is built into Python and comes with every distribution of it I know of – Two-Bit Alchemist Jun 21 '16 at 17:30
  • This is not a duplicate as the answers in the question marked as a duplicate use third-party modules in their answers. – JohnnyP Jun 21 '16 at 17:50
  • I have stated nowhere in this post that datetime is a third-party lib. I have said that I have tried it and have not been able to make it work, such that there is no useful code to post of my trials. – JohnnyP Jun 21 '16 at 17:53

1 Answers1

1

You probably need to study the datetime module capabilities more, but this does what you ask:

>>> d = "2016-06-10T00:00:00Z"
>>> d2 = d.split("-")
>>> d2
['2016', '06', '10T00:00:00Z']
>>> m = ["Jan", "Feb", "Mar", "Apr", "May", "June"]
>>> 
>>> result = m[int(d2[1])-1] + " " + d2[2][:2] + ", " + d2[0]
>>> 
'June 10, 2016'

Of course put all months in m. I didn't feel like typing all 12!

joel goldstick
  • 4,393
  • 6
  • 30
  • 46
  • I have further studies datetime and tried solutions in (http://stackoverflow.com/questions/969285/how-do-i-translate-a-iso-8601-datetime-string-into-a-python-datetime-object that do not use non-native modules and as far as I can tell there is no way to do this using native Python date parsing methods – JohnnyP Jun 21 '16 at 20:58