如何在python中获取当前时间并分解为年、月、日、小时、分钟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30071886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current time in python and break up into year, month, day, hour, minute?
提问by user781486
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?
我想在 Python 中获取当前时间并将它们分配给像year
, month
, day
, hour
, 之类的变量minute
。这如何在 Python 2.7 中完成?
采纳答案by tzaman
The datetime
module is your friend:
该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
You don't need separate variables, the attributes on the returned datetime
object have all you need.
您不需要单独的变量,返回datetime
对象上的属性拥有您需要的一切。
回答by skyline75489
By unpacking timetuple
of datetime object, you should get what you want:
通过解压timetuple
datetime 对象,你应该得到你想要的:
from datetime import datetime
n = datetime.now()
t = n.timetuple()
y, m, d, h, min, sec, wd, yd, i = t
回答by Haresh Shyara
you can use datetime module to get current Date and Time in Python 2.7
您可以使用 datetime 模块在 Python 2.7 中获取当前日期和时间
import datetime
print datetime.datetime.now()
Output :
输出 :
2015-05-06 14:44:14.369392
回答by vossman77
The datetime
answer by tzamanis much cleaner, but you can do it with the original python time
module:
tzaman的datetime
答案要简洁得多,但您可以使用原始的 pythontime
模块来完成:
import time
strings = time.strftime("%Y,%m,%d,%H,%M,%S")
t = strings.split(',')
numbers = [ int(x) for x in t ]
print numbers
Output:
输出:
[2016, 3, 11, 8, 29, 47]
回答by rigsby
Here's a one-liner that comes in just under the 80 char line max.
这是一个单线,最多在 80 个字符线以下。
import time
year, month, day, hour, min = map(int, time.strftime("%Y %m %d %H %M").split())
回答by Bill Bell
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.)
三个用于访问和操作日期和时间的库,即 datetime、arrow 和 pendulum,都使这些项在可通过名称或索引访问其元素的命名元组中可用。此外,可以以完全相同的方式访问这些项目。(我想如果我更聪明,我不会感到惊讶。)
>>> YEARS, MONTHS, DAYS, HOURS, MINUTES = range(5)
>>> import datetime
>>> import arrow
>>> import pendulum
>>> [datetime.datetime.now().timetuple()[i] for i in [YEARS, MONTHS, DAYS, HOURS, MINUTES]]
[2017, 6, 16, 19, 15]
>>> [arrow.now().timetuple()[i] for i in [YEARS, MONTHS, DAYS, HOURS, MINUTES]]
[2017, 6, 16, 19, 15]
>>> [pendulum.now().timetuple()[i] for i in [YEARS, MONTHS, DAYS, HOURS, MINUTES]]
[2017, 6, 16, 19, 16]
回答by TopherGopher
This is an older question, but I came up with a solution I thought others might like.
这是一个较旧的问题,但我想出了一个我认为其他人可能会喜欢的解决方案。
def get_current_datetime_as_dict():
n = datetime.now()
t = n.timetuple()
field_names = ["year",
"month",
"day",
"hour",
"min",
"sec",
"weekday",
"md",
"yd"]
return dict(zip(field_names, t))
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']
.
timetuple() 可以用另一个数组压缩,这会创建标记的元组。将其转换为字典,结果产品可以使用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.
这比这里的其他一些解决方案有更多的开销,但我发现能够在代码中为了清晰而访问命名值真是太好了。
回答by xiaoweiz
import time
year = time.strftime("%Y") # or "%y"
回答by Tajdar Khan Afridi
For python 3
对于蟒蛇 3
import datetime
now = datetime.datetime.now()
print(now.year, now.month, now.day, now.hour, now.minute, now.second)
回答by Sreeragh A R
You can use gmtime
您可以使用 gmtime
from time import gmtime
detailed_time = gmtime()
#returns a struct_time object for current time
year = detailed_time.tm_year
month = detailed_time.tm_mon
day = detailed_time.tm_mday
hour = detailed_time.tm_hour
minute = detailed_time.tm_min
Note: A time stamp can be passed to gmtime, default is current time as returned by time()
注意:时间戳可以传递给 gmtime,默认是 time() 返回的当前时间
eg.
gmtime(1521174681)
See struct_time