Python DateTime%D会导致问题

发布于 01-18 13:18 字数 386 浏览 1 评论 0 原文

我正在尝试处理日期和日期时间

from datetime import date,datetime

d1 = datetime.strptime('2008-03-03','%y-%m-%d')  //the %d is colored different!
d2 = datetime(2008, 2 ,3)

print(date.today())
print(d1 - d2)

错误:

time data '2008-03-03' does not match format '%y-%m-%d'

我在做错什么? 文件的名称为test.py。 多数民众赞成在woule文件,我在vscode中使用rightclick运行它

I'm trying to work with dates and datetime

from datetime import date,datetime

d1 = datetime.strptime('2008-03-03','%y-%m-%d')  //the %d is colored different!
d2 = datetime(2008, 2 ,3)

print(date.today())
print(d1 - d2)

error:

time data '2008-03-03' does not match format '%y-%m-%d'

What I'm doing wrong?
The name of the file is test.py.
Thats the woule file, and I run it with rightclick in VScode 'run python file in terminal'

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

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

发布评论

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

评论(3

屌丝范 2025-01-25 13:18:02

您必须写入%y而不是%y,然后代码变为

d1 = datetime.strptime('2008-03-03','%Y-%m-%d')

You must write %Y not %y, Then the code becomes

d1 = datetime.strptime('2008-03-03','%Y-%m-%d')
喵星人汪星人 2025-01-25 13:18:02

问题在于%y ...

%y年没有一个世纪作为零填充的十进制数字。 00,01,...

99%的年度没有世纪作为小数。 0,1,...,99

%y年,将世纪作为十进制数字。 2013,2019等。

我已经在 <="" a="">,使用修复程序:

from datetime import date,datetime

d1 = datetime.strptime('2008-03-03','%Y-%m-%d')
d2 = datetime(2008, 2 ,3)

print(date.today())
print(d1 - d2)

输出:

2022-04-01 29天,0:00:00

[执行带有退出代码0]

The problem is with the %y...

%y Year without century as a zero-padded decimal number. 00, 01, ...

99 %-y Year without century as a decimal number. 0, 1, ..., 99

%Y Year with century as a decimal number. 2013, 2019 etc.

I've fiddled your code on https://www.mycompiler.io/view/B6EhAzCnd83, with the fix:

from datetime import date,datetime

d1 = datetime.strptime('2008-03-03','%Y-%m-%d')
d2 = datetime(2008, 2 ,3)

print(date.today())
print(d1 - d2)

outputs:

2022-04-01 29 days, 0:00:00

[Execution complete with exit code 0]

鸵鸟症 2025-01-25 13:18:02

It seems like it's an cosmetic problem:

https://github.com/microsoft/vscode/issues/133127

Code works now, no error, but highlighting is still off, even if I use Extension Bisect and deactivate all extensions, or manually deactivate all extensions.

VSCODE 1.66

It seems like it's an cosmetic problem:

https://github.com/microsoft/vscode/issues/133127

Code works now, no error, but highlighting is still off, even if I use Extension Bisect and deactivate all extensions, or manually deactivate all extensions.

VSCode 1.66

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