什么是正确的格式字符串到分析的“ 04.05.2003 00:00:00.000” GMT; 0100 with strptime()?

发布于 2025-01-22 08:04:38 字数 465 浏览 5 评论 0原文

我正在尝试将怪异格式的字符串时间戳转换为DateTime。这就是它的外观:

04.05.2003 00:00:00.000 GMT+0100

我正在尝试这样的事情,但它不匹配。我想我只是错过了GMT倒数的表达。

datetime.datetime.strptime('04.05.2003 00:00:00.000 GMT+0100','%d.%m.%Y HH:MM.SS.sss')

当然,它吐出了格式错误,

ValueError: time data '04.05.2003 00:00:00.000 GMT+0100' does not match format '%d.%m.%Y HH:MM.SS.sss'

我在这里需要使用什么格式字符串?

I am trying to convert a weird format string timestamp to a datetime. This is what it looks like:

04.05.2003 00:00:00.000 GMT+0100

I am trying something like this, but it's not matching. I think I am just missing the expression for that GMT offset at the end.

datetime.datetime.strptime('04.05.2003 00:00:00.000 GMT+0100','%d.%m.%Y HH:MM.SS.sss')

Of course it spits out the format error

ValueError: time data '04.05.2003 00:00:00.000 GMT+0100' does not match format '%d.%m.%Y HH:MM.SS.sss'

What is the format string I need to use here?

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

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

发布评论

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

评论(3

ぽ尐不点ル 2025-01-29 08:04:38

使用%z%z与时区匹配。您正在使用一些奇怪的格式来匹配时间,%h:%m:%S。%f应该适合此

datetime.datetime.strptime('04.05.2003 00:00:00.000 GMT+0100','%d.%m.%Y %H:%M:%S.%f %Z%z')
# Outputs: datetime.datetime(2003, 5, 4, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'GMT'))

Use %Z%z to match the timezone. You were using some weird format for matching the time, %H:%M:%S.%f should be appropriate for that

datetime.datetime.strptime('04.05.2003 00:00:00.000 GMT+0100','%d.%m.%Y %H:%M:%S.%f %Z%z')
# Outputs: datetime.datetime(2003, 5, 4, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'GMT'))
烟─花易冷 2025-01-29 08:04:38

您需要格式运算符,而不是hh:mm:ss,并且需要时区的格式运算符。时区偏移的运算符为%z

datetime.datetime.strptime('04.05.2003 00:00:00.000 GMT+0100','%d.%m.%Y %h:%M:%s.%f GMT%z')

You need format operators for the time, not HH:MM:SS, and you need a format operator for the timezone. The operator for a timezone offset is %z.

datetime.datetime.strptime('04.05.2003 00:00:00.000 GMT+0100','%d.%m.%Y %h:%M:%s.%f GMT%z')
断念 2025-01-29 08:04:38

这应该有效:

datetime.datetime.strptime('04.05.2003 00:00:00.000 GMT+0100','%d.%m.%Y %H:%M:%S.%f GMT%z')

This should work:

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