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
Asked
Active
Viewed 532 times
0
-
1Check out the datetime library: https://stackoverflow.com/questions/466345/converting-string-into-datetime https://docs.python.org/3/library/datetime.html – Eliot K Apr 03 '18 at 20:19
-
How's the weather in the mountain time zone? – ChootsMagoots Apr 03 '18 at 20:20
-
@EliotK that doesn't handle the timezone offset. – Mark Ransom Apr 03 '18 at 20:47
-
It does, you just need to tell it where the timezone is – Eliot K Apr 03 '18 at 23:43
3 Answers
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