错误 - 未支撑的操作数类型(S)用于 - :' str' and' dateTime.timedelta'在Django

发布于 2025-02-08 19:37:46 字数 738 浏览 1 评论 0原文

大家好,我在Django-framework上工作,我是在函数上创建的,在该函数上,我可以在parameter start_date和end_date上,我尝试根据start_date获得上一周的start_date和end_date 错误 - 未支撑的操作数类型 - :'str'和'dateTime.timedelta',请帮助我。

from datetime import datetime, timedelta, date

    def report(request):
        start_date = request.GET.get('start_date')
        print(start_date) # 2022-06-06
        end_date = request.GET.get('end_date')
        print(end_date) # 2022-06-12
        last_week_start_date=start_date - timedelta(days=7)
        last_week_end_date=last_week_start_date + timedelta(days=6)
        d_report = connection.cursor()
        d_report.execute('''select * from table where start_date=%s and end_date=%s''',[start_date,end_date])

Hi Everyone i have working on Django-framework, I am created on function where i give parameter start_date and end_date, i am trying get previous week start_date and end_date based on start_date but getting
Error - unsupported operand type(s) for -: 'str' and 'datetime.timedelta', please help me out.

from datetime import datetime, timedelta, date

    def report(request):
        start_date = request.GET.get('start_date')
        print(start_date) # 2022-06-06
        end_date = request.GET.get('end_date')
        print(end_date) # 2022-06-12
        last_week_start_date=start_date - timedelta(days=7)
        last_week_end_date=last_week_start_date + timedelta(days=6)
        d_report = connection.cursor()
        d_report.execute('''select * from table where start_date=%s and end_date=%s''',[start_date,end_date])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧笙踏歌 2025-02-15 19:37:46

start_date和end_date是字符串。您应该将它们转换为DateTime对象。这应该是ISO格式,因此您可以使用dateTime.fromisoformat()

start_date = request.GET.get('start_date')
start_date = datetime.fromisoformat(start_date)
end_date = request.GET.get('end_date')
end_date = datetime.fromisoformat(end_date)

start_date and end_date are strings. You should convert them to a datetime object. This should be the ISO format so you can use datetime.fromisoformat()

start_date = request.GET.get('start_date')
start_date = datetime.fromisoformat(start_date)
end_date = request.GET.get('end_date')
end_date = datetime.fromisoformat(end_date)
梦在夏天 2025-02-15 19:37:46

问题是start_timestr类型变量。您必须首先将其转换为datetime对象,然后才能将其使用。

要转换,您可以使用此代码库

from datetime import datetime

datetime_object = datetime.strptime(start_time, '%Y-%m-%d')

The problem is start_time is a str type variable. You have to convert it to datetime object first, only then you can use timedelta with that.

To convert, you can use this codebase

from datetime import datetime

datetime_object = datetime.strptime(start_time, '%Y-%m-%d')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文