-9

I have next time value in unicode (<type 'unicode'>):

2017-08-09T15:02:58+0000.

How to convert it to friendly view (e.g. Day, Month of Year)?

Den Silver
  • 199
  • 16
  • Try the answer from another thread from stack [ this site.](https://stackoverflow.com/questions/311627/how-to-print-date-in-a-regular-format-in-python) – chebad Aug 30 '17 at 10:18
  • Before posting, ***you are expected to have researched your issue and made a good attempt to write the code yourself***. Please read [How much research effort is expected of Stack Overflow users](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – FluffyKitten Aug 30 '17 at 15:02

2 Answers2

1

This should do what you ask:

from datetime import datetime

a = '2017-08-09T15:02:58+0000'

datetime.strptime(a[:-5], '%Y-%m-%dT%H:%M:%S').strftime('%d, %b of %Y')
#09, Aug of 2017

strptime method throws error for timezone parameter that doesn't seem to interest you so I removed that part with a[:-5].

For the rest of the string you can just follow guidelines from datetime docs.

Using the same docs you can construct your datetime string using strftime() method like you wanted '%d, %b of %Y' or in plain words [day], [abbreviated month] of [Year]

zipa
  • 27,316
  • 6
  • 40
  • 58
  • Code-only answers are often unhelpful in pointing to why the issue happened. You should include an explanation why your code solves the issue. Please read [How do I write a good answer](https://stackoverflow.com/help/how-to-answer) – FluffyKitten Aug 30 '17 at 14:19
  • @FluffyKitten You are right, poorly written question shouldn't imply poorly written answer. – zipa Aug 30 '17 at 14:36
  • The other answer was flagged for this reason, so I thought it only fair to add the same comment to yours during my review :) – FluffyKitten Aug 30 '17 at 15:01
  • That sounds fair to me :) – zipa Aug 30 '17 at 15:09
0

try this

import datetime
today = datetime.date.today()
print today.strftime('We are the %d, %b %Y')


'We are the 22, Nov 2008'
Mehdi Ben Hamida
  • 893
  • 4
  • 16
  • 38
  • 1
    Code-only answers are often unhelpful in pointing to *why* the issue happened. You should include an explanation why your code solves the issue. Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – FluffyKitten Aug 30 '17 at 12:09
  • he doesn't have an issue, he just wanted a code. read the question – Mehdi Ben Hamida Aug 30 '17 at 14:06
  • Asking for code is against Stack Overflow's ethos - if you read [How do I write a good answer](https://stackoverflow.com/help/how-to-answer) as I linked to, you'd see that low quality questions shouldn't be answered in the first place. Also, assuming this question isn't deleted for low quality, your answer is also meant to help others with the same question, so you should explain your code. You are relatively new to this site, so please review the [help center](https://stackoverflow.com/help) and check [StackOverflow Meta](https://meta.stackoverflow.com/) for guidance on using it effectively. – FluffyKitten Aug 30 '17 at 14:18