如何在python中获取当前时间并分解为年,月,日,小时,分钟?


Answers:


551

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对象上的属性就可以满足您的所有需求。


4
只是一个惊喜:import time \n now=time.localtime() \n print now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_hour, now.tm_min, now.tm_sec, now.tm_wday, now.tm_yday, now.tm_isdst
Sean.H

31

这是一个单线,最大字符数不超过80个字符。

import time
year, month, day, hour, min = map(int, time.strftime("%Y %m %d %H %M").split())

29

tzamandatetime答案要干净得多,但是您可以使用原始的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]

14

通过解压缩timetupledatetime对象,您应该得到想要的东西:

from datetime import datetime

n = datetime.now()
t = n.timetuple()
y, m, d, h, min, sec, wd, yd, i = t

6

对于python 3

import datetime
now = datetime.datetime.now()
print(now.year, now.month, now.day, now.hour, now.minute, now.second)

6

让我们看看如何从当前时间获取并打印python中的日,月,年:

import datetime

now = datetime.datetime.now()
year = '{:02d}'.format(now.year)
month = '{:02d}'.format(now.month)
day = '{:02d}'.format(now.day)
hour = '{:02d}'.format(now.hour)
minute = '{:02d}'.format(now.minute)
day_month_year = '{}-{}-{}'.format(year, month, day)

print('day_month_year: ' + day_month_year)

结果:

day_month_year: 2019-03-26

3
import time
year = time.strftime("%Y") # or "%y"

2

三个用于访问和操纵日期和时间的库,即日期时间,箭头和摆锤,都使这些项在命名元组中可用,命名元组的元素可通过名称或索引访问。此外,可以完全相同的方式访问项目。(我想如果我更聪明,我不会感到惊讶。)

>>> 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]

2

您可以使用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

注意:可以将时间戳传递给gmtime,默认为time()返回的当前时间

eg.
gmtime(1521174681)

参见struct_time


1

这是一个比较老的问题,但是我想出了一个我认为其他人可能会喜欢的解决方案。

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()可以与另一个数组一起压缩,这将创建带标签的元组。将其转换为字典,然后可以使用生成的产品get_current_datetime_as_dict()['year']

这比这里的其他一些解决方案有更多的开销,但是我发现能够在代码中为清楚起见而访问命名值真是太好了。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.