我想在Python语言中获取当前时间,并将其赋值为变量,如year
、month
、day
、hour
、minute
。在Python2.7中如何做到这一点呢?
发布于 2015-05-06 16:56:36
datetime
模块是您的好朋友:
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
对象上的属性具有您需要的所有内容。
发布于 2016-12-21 04:28:21
这是一个最多80个字符的一行代码。
import time
year, month, day, hour, min = map(int, time.strftime("%Y %m %d %H %M").split())
发布于 2016-03-11 22:31:46
tzaman的datetime
答案要简洁得多,但您可以使用原始的python time
模块来完成:
import time
strings = time.strftime("%Y,%m,%d,%H,%M,%S")
t = strings.split(',')
numbers = [ int(x) for x in t ]
print numbers
输出:
[2016, 3, 11, 8, 29, 47]
https://stackoverflow.com/questions/30071886
复制相似问题