1
Trim21 2020-09-02 19:29:41 +08:00 via Android
用 datetime ( 0 点)循环加 timedelta ( hours=1 ),直到大于当前时间,然后把符合条件的的序列化成你想要的格式
|
2
Eyon OP @Trim21 谢谢
```python from datetime import datetime,timedelta now = datetime.now() thismonth_start = now.strftime('%Y%m01') start = datetime(2020,9,1) while True: if start < now: print(start.strftime('%Y%m%d%H')) start = start+timedelta(hours=1) ``` |
3
tairan2006 2020-09-02 23:16:22 +08:00 via Android
zfill 函数了解一下
|
4
TEwrc 2020-09-03 16:37:32 +08:00
@tairan2006 看了一下没太懂 请教一下怎么用到题主说的这个问题上呢?
|
5
tairan2006 2020-09-03 17:31:25 +08:00
@TEwrc
now = datetime.now() prefix = now.strftime('%Y%m%d') return [prefix + str(x).zfill(2) for x in range(now.hour)] |
6
TEwrc 2020-09-04 12:00:52 +08:00
@tairan2006 秒啊!感谢
|
7
biglazycat 2020-09-11 23:03:28 +08:00
from datetime import datetime
time_list = [] now = datetime.now() start_day_hour = int(now.strftime('%Y%m%d00')) stop_day_hour = int(now.strftime('%Y%m%d%H')) for i in range(start_day_hour, stop_day_hour + 1): time_list.append(i) print(time_list) |