gpt4 book ai didi

python - 我如何在 Python 中获取明天的日期?

转载 作者:太空宇宙 更新时间:2023-11-04 08:16:33 25 4
gpt4 key购买 nike

我可以使用 Python 脚本获取今天的日期和时间。但我需要以数字格式获取明天的日期和时间。所以我使用了下面的脚本。

#! /usr/bin/python
import datetime
import sys
import os

tomorrow = datetime.date.today() + datetime.timedelta(days=1)
print "Tomorrow date is" + str(tomorrow)
tm_stme = datetime.time(0, 0, 0)
tm_etime = datetime.time(23,59,59)
tm_stdate = datetime.datetime.combine(tomorrow, tm_stme)
tm_enddate = datetime.datetime.combine(tomorrow,tm_etime)

print "tomorrow start date:" + tm_stdate
print "tomorrow end date:" + tm_enddate

tm_sdt_convert = time.mktime(time.strptime(tm_stdate, "%Y-%m-%d %H:%M:%S"))
tm_tdt_convert = time.mktime(time.strptime(tm_enddate, "%Y-%m-%d %H:%M:%S"))

但它抛出以下错误:

administrator@Ashok-Dev:~/Desktop$ python testing.py
Tomorrow date is2012-11-24
Traceback (most recent call last):
File "testing.py", line 13, in <module>
print "tomorrow start date:" + tm_stdate
TypeError: cannot concatenate 'str' and 'datetime.datetime' objects

我需要获取 tm_sdt_convert 作为可变结果。如何获得无错误的输出?

最佳答案

这样的事情可以帮助你

In [125]: datetime.datetime.now()
Out[125]: datetime.datetime(2012, 11, 23, 17, 11, 15, 765000)

In [126]: str(datetime.datetime.now() + datetime.timedelta(days=1))
Out[126]: '2012-11-24 17:11:18.203000'

得到个别的东西

In [127]: (datetime.datetime.now() + datetime.timedelta(days=1)).day
Out[127]: 24

In [128]: (datetime.datetime.now() + datetime.timedelta(days=1)).year
Out[128]: 2012

In [129]: (datetime.datetime.now() + datetime.timedelta(days=1)).month
Out[129]: 11

关于python - 我如何在 Python 中获取明天的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13528287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com