推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Eyon
V2EX  ›  Python

Python 如何生成今天 0 点到现在的所有以小时为单位的时间字符串? and ...

  •  
  •   Eyon · Sep 2, 2020 · 2883 views
    This topic created in 2106 days ago, the information mentioned may be changed or developed.
    需求:

    get_today = [2020090200,2020090201,2020090202...2020090219]

    get_month=[2020090100,2020090101,2020090102......2020090219]
    7 replies    2020-09-11 23:03:28 +08:00
    Trim21
        1
    Trim21  
       Sep 2, 2020 via Android
    用 datetime ( 0 点)循环加 timedelta ( hours=1 ),直到大于当前时间,然后把符合条件的的序列化成你想要的格式
    Eyon
        2
    Eyon  
    OP
       Sep 2, 2020
    @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)
    ```
    tairan2006
        3
    tairan2006  
       Sep 2, 2020 via Android
    zfill 函数了解一下
    TEwrc
        4
    TEwrc  
       Sep 3, 2020
    @tairan2006 看了一下没太懂 请教一下怎么用到题主说的这个问题上呢?
    tairan2006
        5
    tairan2006  
       Sep 3, 2020
    @TEwrc

    now = datetime.now()
    prefix = now.strftime('%Y%m%d')
    return [prefix + str(x).zfill(2) for x in range(now.hour)]
    TEwrc
        6
    TEwrc  
       Sep 4, 2020
    @tairan2006 秒啊!感谢
    biglazycat
        7
    biglazycat  
       Sep 11, 2020
    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)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3082 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 15:09 · PVG 23:09 · LAX 08:09 · JFK 11:09
    ♥ Do have faith in what you're doing.