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
This is an older question, but I came up with a solution I thought others might like.
timetuple() can be zipped with another array, which creates labeled tuples. Cast that to a dictionary and the resultant product can be consumed with
get_current_datetime_as_dict()['year']
.This has a little more overhead than some of the other solutions on here, but I've found it's so nice to be able to access named values for clartiy's sake in the code.
The
datetime
answer above is much cleaner, but you can do it with the original pythontime
module:Output:
you can use datetime module to get current Date and Time in Python 2.7
Output :