I would like to get the current time in Python and assign them into variables like year
, month
, day
, hour
, minute
. How can this be done in Python 2.7?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Three libraries for accessing and manipulating dates and times, namely datetime, arrow and pendulum, all make these items available in namedtuples whose elements are accessible either by name or index. Moreover, the items are accessible in precisely the same way. (I suppose if I were more intelligent I wouldn't be surprised.)
For python 3
You can use gmtime
Note: A time stamp can be passed to gmtime, default is current time as returned by time()
See struct_time
The
datetime
module is your friend:You don't need separate variables, the attributes on the returned
datetime
object have all you need.Here's a one-liner that comes in just under the 80 char line max.
By unpacking
timetuple
of datetime object, you should get what you want: