7

I have this string '2015-04-08T07:52:00Z' and I wanna to convert it to '08/04/2015', how can I do this?

Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
Natali Torres
  • 303
  • 1
  • 4
  • 13

2 Answers2

10

You can use the datetime.datetime.strptime() function to create a datetime object, then datetime.datetime.strftime() to return your correctly formatted date like so:

from datetime import datetime
dt = datetime.strptime('2015-04-08T07:52:00Z', '%Y-%m-%dT%H:%M:%SZ')
print dt.strftime('%d/%m/%Y')
Vorticity
  • 4,582
  • 4
  • 32
  • 49
1

You can use easy_date to make it easy:

import date_converter
converted_date = date_converter.string_to_string('2015-04-08T07:52:00Z', '%Y-%m-%dT%H:%M:%SZ', '%d/%m/%Y')
Raphael Amoedo
  • 4,233
  • 4
  • 28
  • 37
  • 1
    you should probably use a single namespace e.g., `easy_date.string_to_string` (create a package and import the corresponding names, see how [`asyncio`](https://hg.python.org/cpython/file/9dfda4e7169a/Lib/asyncio/__init__.py) or `numpy` do it). – jfs May 09 '15 at 21:45