python minidom firstChild.data ignore part of string (date + time + timezone) drop timezone

StackOverflow https://stackoverflow.com/questions/23661561

  •  22-07-2023
  •  | 
  •  

Question

<Since>2014-04-23 13:06:32 America/New_York</Since>

I need to get just the date and time from the Element into one variable. run_time_info

My code:

run_time_info = thread_id.getElementsByTagName("Since")[0].firstChild.data
Was it helpful?

Solution

You can also use dateutil to parse the string directly into datetime by passing fuzzy=True to the parse() method:

>>> from dateutil import parser
>>> s = "2014-04-23 13:06:32 America/New_York"
>>> run_time_info = parser.parse(s, fuzzy=True)
>>> run_time_info
datetime.datetime(2014, 4, 23, 13, 6, 32)

OTHER TIPS

If the data is always in the exact same format (date-space-time-space-location), you can use conventional string methods to split and grab components from the string:

data = thread_id.getElementsByTagName("Since")[0].firstChild.data
date, time, location = data.split(' ')
run_time_info = date + ' ' + time
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top