如何将 python 日期时间转换为具有可读格式日期的字符串?

发布于 2024-08-19 16:48:00 字数 220 浏览 5 评论 0原文

t = e['updated_parsed']
dt = datetime.datetime(t[0],t[1],t[2],t[3],t[4],t[5],t[6]
print dt
>>>2010-01-28 08:39:49.000003

如何将其转换为字符串?:

"January 28, 2010"
t = e['updated_parsed']
dt = datetime.datetime(t[0],t[1],t[2],t[3],t[4],t[5],t[6]
print dt
>>>2010-01-28 08:39:49.000003

How do I turn that into a string?:

"January 28, 2010"

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

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

发布评论

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

评论(9

谁对谁错谁最难过 2024-08-26 16:48:01

datetime 类有一个方法 strftime。 Python 文档记录了它接受的不同格式: strftime() 和 strptime () 行为

对于这个具体示例,它看起来像:

my_datetime.strftime("%B %d, %Y")

The datetime class has a method strftime. The Python docs documents the different formats it accepts: strftime() and strptime() Behavior

For this specific example, it would look something like:

my_datetime.strftime("%B %d, %Y")
月光色 2024-08-26 16:48:01

以下是如何使用 python 的通用格式化函数来完成相同的任务...

>>>from datetime import datetime
>>>"{:%B %d, %Y}".format(datetime.now())

此处使用的格式化字符与 strftime。不要错过格式说明符中的前导 :

在大多数情况下,使用 format() 代替 strftime() 可以使代码更具可读性,更易于编写并与生成格式化输出的方式保持一致...将

>>>"{} today's date is: {:%B %d, %Y}".format("Andre", datetime.now())

上面的内容与以下 strftime() 替代方案进行比较...

>>>"{} today's date is {}".format("Andre", datetime.now().strftime("%B %d, %Y"))

此外,以下是行不通的...

>>>datetime.now().strftime("%s %B %d, %Y" % "Andre")
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    datetime.now().strftime("%s %B %d, %Y" % "Andre")
TypeError: not enough arguments for format string

等等...

Here is how you can accomplish the same using python's general formatting function...

>>>from datetime import datetime
>>>"{:%B %d, %Y}".format(datetime.now())

The formatting characters used here are the same as those used by strftime. Don't miss the leading : in the format specifier.

Using format() instead of strftime() in most cases can make the code more readable, easier to write and consistent with the way formatted output is generated...

>>>"{} today's date is: {:%B %d, %Y}".format("Andre", datetime.now())

Compare the above with the following strftime() alternative...

>>>"{} today's date is {}".format("Andre", datetime.now().strftime("%B %d, %Y"))

Moreover, the following is not going to work...

>>>datetime.now().strftime("%s %B %d, %Y" % "Andre")
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    datetime.now().strftime("%s %B %d, %Y" % "Andre")
TypeError: not enough arguments for format string

And so on...

终止放荡 2024-08-26 16:48:01

在 Python 3.6+ 中使用 f 字符串。

from datetime import datetime

date_string = f'{datetime.now():%Y-%m-%d %H:%M:%S%z}'
date_string
# returns '2024-01-15 23:58:18'
LogTimeFormat = f'{datetime.utcnow():%Y%m%d_%H%M%S}'
LogTimeFormat
# returns time in UTC '20240116_055907'

Using f-strings, in Python 3.6+.

from datetime import datetime

date_string = f'{datetime.now():%Y-%m-%d %H:%M:%S%z}'
date_string
# returns '2024-01-15 23:58:18'
LogTimeFormat = f'{datetime.utcnow():%Y%m%d_%H%M%S}'
LogTimeFormat
# returns time in UTC '20240116_055907'
束缚m 2024-08-26 16:48:01

我知道,这是一个很老的问题。但随着新的 f-strings (从 python 3.6 开始),有新鲜的选项。为了完整起见,这里:

from datetime import datetime

dt = datetime.now()

# str.format
strg = '{:%B %d, %Y}'.format(dt)
print(strg)  # July 22, 2017

# datetime.strftime
strg = dt.strftime('%B %d, %Y')
print(strg)  # July 22, 2017

# f-strings in python >= 3.6
strg = f'{dt:%B %d, %Y}'
print(strg)  # July 22, 2017

strftime() 和 strptime() 行为解释了格式说明符的含义。

very old question, i know. but with the new f-strings (starting from python 3.6) there are fresh options. so here for completeness:

from datetime import datetime

dt = datetime.now()

# str.format
strg = '{:%B %d, %Y}'.format(dt)
print(strg)  # July 22, 2017

# datetime.strftime
strg = dt.strftime('%B %d, %Y')
print(strg)  # July 22, 2017

# f-strings in python >= 3.6
strg = f'{dt:%B %d, %Y}'
print(strg)  # July 22, 2017

strftime() and strptime() Behavior explains what the format specifiers mean.

久光 2024-08-26 16:48:01

Python datetime 对象有一个 method 属性,它以可读的格式打印。

>>> a = datetime.now()
>>> a.ctime()
'Mon May 21 18:35:18 2018'
>>> 

Python datetime object has a method attribute, which prints in readable format.

>>> a = datetime.now()
>>> a.ctime()
'Mon May 21 18:35:18 2018'
>>> 
凹づ凸ル 2024-08-26 16:48:01

从官方文档中阅读 strfrtime

Read strfrtime from the official docs.

屋顶上的小猫咪 2024-08-26 16:48:01

对于那些急于阅读官方文档的人 strftime:)

from datetime import datetime
datetime.now().strftime("%H:%M %B %d, %Y")
'12:11 August 08, 2022'

For those who are impatient to read the nice official docs strftime :)

from datetime import datetime
datetime.now().strftime("%H:%M %B %d, %Y")
'12:11 August 08, 2022'
囚我心虐我身 2024-08-26 16:48:01

要获取符合区域设置的字符串,可以使用 %c

import datetime
import locale

locale.setlocale(locale.LC_TIME, "de_DE")

now = datetime.datetime.now()

now.strftime("%c")

"Mi 26 Jun 17:17:23 2024"

参考:strftime() 和 strptime() 格式代码

To get a string that respects the locale, you can use %c.

import datetime
import locale

locale.setlocale(locale.LC_TIME, "de_DE")

now = datetime.datetime.now()

now.strftime("%c")

"Mi 26 Jun 17:17:23 2024"

Reference: strftime() and strptime() Format Codes

何以心动 2024-08-26 16:48:01

这是为了格式化日期?

def format_date(day, month, year):
        # {} betekent 'plaats hier stringvoorstelling van volgend argument'
        return "{}/{}/{}".format(day, month, year)

This is for format the date?

def format_date(day, month, year):
        # {} betekent 'plaats hier stringvoorstelling van volgend argument'
        return "{}/{}/{}".format(day, month, year)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文