Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Split date and time string

Hi,
I'm trying to split "2018-02-04T21:34:00+01:00" into "21:34", so I can only see the timestamp without date etc. How is this possible?

Thanks in advance.
from time import gmtime, strftime
strftime("%H:%M", gmtime())

you mean ?
(Feb 4, 2018 01:44 PM)lmfao3zWin Wrote:  from time import gmtime, strftime
strftime("%H:%M", gmtime())

you mean ?

Something like this is ideal. Instead of messing with the format, parse it and just extract what you need.
Take a look into dateutil as I'm not sure there is a way to parse it like that in a simple way. Please correct me if I am wrong.

PHP Code

<?php 
>>> from dateutil import parser
>>> dt = parser.parse("2018-02-04T21:34:00+01:00")
>>> 
"{}:{}".format(dt.hour, dt.minute)
'21:34' 

hi
(Feb 4, 2018 02:31 PM)Anxuein Wrote:  Take a look into dateutil as I'm not sure there is a way to parse it like that in a simple way. Please correct me if I am wrong.

PHP Code

<?php 
>>> from dateutil import parser
>>> dt = parser.parse("2018-02-04T21:34:00+01:00")
>>> 
"{}:{}".format(dt.hour, dt.minute)
'21:34' 

This worked. Thanks a lot!
You could also do
Code
time = date_time_string.split(“T”)[1].split(“+“)[0]
but since you’re using the built in class just do what others suggested


Possibly Related Threads...
Thread / Author Replies Views Last Post
[The Brotherhood] by HackersApr 10, 2026 02:15 AM 22 2,161 Apr 10, 2026 02:15 AM
Last Post: PELock
by KlonopinApr 10, 2026 02:08 AM 10 651 Apr 10, 2026 02:08 AM
Last Post: PELock
[C#] by BBGMazeJun 2, 2022 06:17 PM 2 166 Jun 2, 2022 06:17 PM
Last Post: Prisonhack9279