Extracting minutes and seconds from DataTime object

This is the place for queries that don't fit in any of the other categories.

Extracting minutes and seconds from DataTime object

Postby fbartolom » Thu Jan 30, 2014 12:42 pm

I installed python 3.3 possessing the DateTime module and wanted to parse the date time I receive from a remote source to extract its minutes and seconds. When I print it directly it comes in the form of:
20140130T13:37:00
but whatever attribute I ask I have an error.
AttributeError: 'DateTime' object has no attribute 'time'

In particular by asking its attributes with dir I got the following strange list:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'decode', 'encode', 'make_comparable', 'timetuple', 'value']
How do I get from there to either a string to be parsed with strftime or directly to the values I am looking for?

Thanks,
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Extracting minutes and seconds from DataTime object

Postby Yoriz » Thu Jan 30, 2014 12:54 pm

Try printing
Code: Select all
yourobject.timetuple

if that only prints an object try
Code: Select all
yourobject.timetuple()

and see what you get.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Passig a dictionary from python to php

Postby Kebap » Thu Jan 30, 2014 12:54 pm

You can ignore those starting with underscores, so only 5 remain.

Also check out the python documentation:
* http://docs.python.org/2/library/datetime.html#datetime-objects
* http://docs.python.org/2/library/xmlrpclib.html#datetime-objects

By the way, a link to the original thread with more background info: http://www.python-forum.org/viewtopic.php?f=22&t=10753
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 382
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Extracting minutes and seconds from DataTime object

Postby fbartolom » Thu Jan 30, 2014 1:28 pm

print (start.timetuple()) returns:

time.struct_time(tm_year=2014, tm_mon=1, tm_mday=30, tm_hour=14, tm_min=25, tm_sec=0, tm_wday=3, tm_yday=30, tm_isdst=-1)

How do I read them?
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Extracting minutes and seconds from DataTime object

Postby fbartolom » Thu Jan 30, 2014 1:43 pm

I think I managed it with:
Code: Select all
s = datetime.datetime.strptime(start.value, "%Y%m%dT%H:%M:%S")
print ("partenza: ",s.strftime('%H:%M'))
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest