0

2018-04-03T14:10:00-07:00 is there a built in python method? Or something in Django? I am only used to working with javascript/node and I can't figure this out

watthecode
  • 41
  • 1
  • 1
  • 5

3 Answers3

1

Please try the code below:

from dateutil.parser import parse

parse('2018-04-03T14:10:00-07:00').strftime('%d-%m-%Y %H:%M:%f')

Output

'03-04-2018 14:10:000000'

You can pass the specific format you want out of the date. for reference please go to this document: Strftime options

Prateek
  • 1,538
  • 13
  • 22
0

I didn't see any specific single function to handle that format, but tweaking the text a little and using a custom format will get you what you need.

from datetime import datetime 
txt = "2018-04-03T14:10:00-07:00"
txtfmt = txt[:10]+ " " + txt[11:19] + " " + txt[19:22] + txt[-2:]
dt = datetime.strptime(txtfmt,"%Y-%m-%d %H:%M:%S %z")

Hope it helps!

Eliot K
  • 614
  • 5
  • 16
0

You might want to give Arrow a try.

>>> import arrow
>>> d = arrow.get('2018-04-03T14:10:00-07:00')
>>> d
<Arrow [2018-04-03T14:10:00-07:00]>
>>> d.humanize()
'3 hours ago'
>>> d.format()
'2018-04-03 14:10:00-07:00'
>>>

For Django you can use it with django-arrow-field for easier access.

kichik
  • 33,220
  • 7
  • 94
  • 114