1

It doesn’t get parsed and just prints out the same ISO string that I’m trying to parse: 2/12/17 00:00:52

What could I be doing wrong? Thank you in advance and will be sure to vote up and accept the answer

4 Answers 4

1

This happens because the representation of a datetime variable (that is the result of dateutil.parser.parse, btw) is to print the ISO representation of the date.

However, if you store the variable instead of simply printing it after parsing it, you can print each individual part of the date:

date = dateutil.parser.parse("2017-02-16 22:11:05+00:00")
print(date.year)
2017

print(date.month)
2

print(date.day)
16

print(date.hour)
22

print(date.minute)
11

print(date.second)
5

Cheers!

Sign up to request clarification or add additional context in comments.

Comments

0

What do you mean it didn't parse... it did but then you asked to print it back out again it turns it back into a string:

>>> import dateutil.parser
>>> date = dateutil.parser.parse("2017-02-16 22:11:05+00:00")
>>> repr(date)
'datetime.datetime(2017, 2, 16, 22, 11, 5, tzinfo=tzutc())'
>>> date.timetuple()
time.struct_time(tm_year=2017, tm_mon=2, tm_mday=16, tm_hour=22, 
                 tm_min=11, tm_sec=5, tm_wday=3, tm_yday=47, tm_isdst=0)
>>> str(date)
2017-02-16 22:11:05+00:00

Comments

0

I'll shamefully plug the library I use for datetime, pendulum.

import pendulum

parsed_time = pendulum.parse('2017-02-16 22:11:05+00:00')

parsed_time.to_formatted_date_string()
'Feb 16, 2017'

Theres a bunch more options as well, makes working with date time super easy.

Comments

0

If you would like to print it with only year, month, day, hour, minutes, and seconds, you could do this:

x=dateutil.parser.parse("2017-02-16 22:11:05+00:00")
print ("<%s>") % (x.strftime('%Y-%m-%d at %H:%M:%S'))
# Output <2017-02-16 at 22:11:05>

Comments