python从时间开始获得异型日期

发布于 2025-02-10 02:38:44 字数 249 浏览 1 评论 0原文

我有此字符串:

date_string =“ 2022-04-11 00:00:00.000000”

,我试图转换为此格式:

“ 2022-04-11t00:00:00.000 Z”

date = 我尝试这样做的同样

,但是像无效格式一样遇到错误

datetime.strptime(date_string, "%Y-%m-%dT %H:%M:%S%z")

I have this string:

date_string = "2022-04-11 00:00:00.000000"

which I'm trying to convert to this format:

date = "2022-04-11T00:00:00.000Z"

Can anyone please help me with the same

i tried to do this but am getting Error like invalid format

datetime.strptime(date_string, "%Y-%m-%dT %H:%M:%S%z")

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

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

发布评论

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

评论(1

我恋#小黄人 2025-02-17 02:38:44

有一个被称为dateutil的图书馆。
在此的帮助下,我们将字符串解析为日期格式,然后将其转换为ISOUMAT。

导入dateutil.parser作为解析器

date_string =“ 2022-04-11 00:00:00.000000:00.000000”

date = parser.parser.parse.parse(date_string)

< < < <代码> print(date.strftime('%y-%m-%dt%h:%m:%sz'))

输出将为
2022-04-11T00:00:00Z

There's a library known as dateutil.
With the help of that, we parse the string into date format and then we convert it into the isoformat.

import dateutil.parser as parser

date_string = "2022-04-11 00:00:00.000000"

date = parser.parse(date_string)

print(date.strftime('%Y-%m-%dT%H:%M:%SZ'))

The output would be
2022-04-11T00:00:00Z

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