V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
Ne
V2EX  ›  Python

新人小白请问各位大佬,在 data 中的日期变量,应该怎样写才对 ?

  •  
  •   Ne · 2022-08-24 01:05:02 +08:00 · 1625 次点击
    这是一个创建于 583 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import datetime

    now_time = datetime.datetime.now()
    yesterday = now_time + datetime.timedelta(days = -1)
    begin_time = yesterday.strftime('%Y-%m-%d 08:00:00')
    end_time = now_time.strftime('%Y-%m-%d 07:59:59')

    data = {
    'inTimeStart': '2022-08-18 08:00:00', # 开始时间
    'inTimeEnd': '2022-08-19 07:59:59', # 结束时间

    上面的运行正常


    下面这样的会出错

    data = {
    'inTimeStart': 'begin_time', # 开始时间
    'inTimeEnd': 'end_time', # 结束时间
    2 条回复    2022-08-24 21:16:46 +08:00
    eason1874
        1
    eason1874  
       2022-08-24 01:30:08 +08:00   ❤️ 1
    后面是变量吧,把引号去掉,像这样

    'inTimeStart': begin_time,
    'inTimeEnd': end_time
    Ne
        2
    Ne  
    OP
       2022-08-24 21:16:46 +08:00
    @eason1874 感谢你!,按你说这样做可以。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5784 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 06:23 · PVG 14:23 · LAX 23:23 · JFK 02:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.