如何获取当前时间在python并分解成年,月,日,小时,分钟?
问题描述:
我想在Python中获得当前时间,并将其分配给变量,如 year
, month
,天
,小时
,分
。如何在Python 2.7中完成?
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?
答
datetime
模块是你的朋友:
The datetime
module is your friend:
import datetime
now = datetime.datetime.now()
print now.year, now.month, now.day, now.hour, now.minute, now.second
# 2015 5 6 8 53 40
您不需要单独的变量,返回的 datetime
对象的属性都需要您。
You don't need separate variables, the attributes on the returned datetime
object have all you need.