除以下方法外,是否有更好的方式以 YYYY-MM-DD
的格式返回今天的日期?
str(datetime.datetime.today()).split()[0]
除以下方法外,是否有更好的方式以 YYYY-MM-DD
的格式返回今天的日期?
str(datetime.datetime.today()).split()[0]
使用 strftime
函数:
>>> from datetime import datetime
>>> datetime.today().strftime('%Y-%m-%d')
'2021-01-26'
为了在末尾也包含一个零填充的小时:分钟:秒
:
为了在末尾也包含一个零填充的小时:分钟:秒
:
>>> datetime.today().strftime('%Y-%m-%d %H:%M:%S')
'2021-01-26 16:50:03'
获取UTC日期和时间:
>>> datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
'2021-01-27 00:50:03'
time.strftime("%Y-%m-%d")
怎么样? - aderchoxdatetime.utcnow().strftime('%Y-%m-%d')
。 - Agost Birodate
和 datetime
都有这个功能。 - dummyDevdatetime.today().strftime("%Y%m%d-%H%M%S.%f")
。示例输出:'20230803-195136.416752'
。它的含义是年月日-时分秒.微秒
,即"年 月 日 - 时 分 秒.微秒"。我经常用它来自动排序日志文件和临时文件。 - Gabriel Staplesdatetime.date.today()
函数,并将其返回的datetime.date
对象转换为字符串:from datetime import date
today = str(date.today())
print(today) # '2017-12-26'
date.today().strftime('%Y/%m/%d')
。 - kmonsoor.
表示“年.月.日”,分隔符/
表示“月/日/年”,分隔符-
表示“年-月-日”。虽然不是每个人都遵循这些准则,但只要不是每个人都切换到“年-月-日”,遵循这些国际日期阅读规范会有所帮助。 - glglgl我总是使用isoformat()
方法来做这件事。
from datetime import date
today = date.today().isoformat()
print(today) # '2018-12-05'
datetime
对象。from datetime import datetime
now = datetime.today().isoformat()
print(now) # '2018-12-05T11:15:55.126382'
import time
today = time.strftime("%Y-%m-%d")
# 2023-10-14
如果你喜欢记住有趣的代码,那么Datetime很可爱。但是,你不希望更简单吗?
>>> import arrow
>>> arrow.now().format('YYYY-MM-DD')
'2017-02-17'
这个模块足够聪明,可以理解你的意思。
只需执行pip install arrow
。
附言:回应那些对这个答案感到激动的人,我想说的是arrow代表了Python中处理日期的替代方案之一。这基本上是我想建议的。
print(date.today())
更好?哈哈,而且可以轻松获取整数值t.year、t.month、t.day+1
,其中t = date.today()
,人们也不必打绿箭头来告诉他们时间。哦天啊,这段代码太难记了。 - Puddleimport datetime
和 import arrow
有什么不同?当然,一个是内置的,但如果 arrow
提供了更方便的格式,为什么不使用它呢? - information_interchangearrow
。但是对于许多简单的项目来说,这只是不必要的。此外,在许多公司内部项目中,人们需要获得批准才能包含新的库。 - kmonsoor你可以使用pandas库中的pd.to_datetime
。根据你想要返回的内容,这里有各种不同的选项。
import pandas as pd
pd.to_datetime('today') # pd.to_datetime('now')
# Timestamp('2019-03-27 00:00:10.958567')
pd.to_datetime('today').to_pydatetime()
# datetime.datetime(2019, 4, 18, 3, 50, 42, 587629)
pd.to_datetime('today').isoformat()
# '2019-04-18T04:03:32.493337'
# Or, `strftime` for custom formats.
pd.to_datetime('today').strftime('%Y-%m-%d')
# '2019-03-27'
要从时间戳中仅获取日期,请调用Timestamp.date
。
pd.to_datetime('today').date()
# datetime.date(2019, 3, 27)
除了 to_datetime
外,您还可以直接使用以下方法实例化 Timestamp
对象:
pd.Timestamp('today') # pd.Timestamp('now')
# Timestamp('2019-04-18 03:43:33.233093')
pd.Timestamp('today').to_pydatetime()
# datetime.datetime(2019, 4, 18, 3, 53, 46, 220068)
如果您想使时间戳具有时区感知能力,请将时区传递给tz
参数。
pd.Timestamp('now', tz='America/Los_Angeles')
# Timestamp('2019-04-18 03:59:02.647819-0700', tz='America/Los_Angeles')
我保证这个很好用。
如果你正在使用pendulum,有一些有趣的选择。你可以使用now()
获取当前时间戳,或者使用today()
获取今天的日期。
import pendulum
pendulum.now()
# DateTime(2019, 3, 27, 0, 2, 41, 452264, tzinfo=Timezone('America/Los_Angeles'))
pendulum.today()
# DateTime(2019, 3, 27, 0, 0, 0, tzinfo=Timezone('America/Los_Angeles'))
此外,您还可以直接获取tomorrow()
或yesterday()
的日期,而无需进行任何额外的时间差算术运算。
pendulum.yesterday()
# DateTime(2019, 3, 26, 0, 0, 0, tzinfo=Timezone('America/Los_Angeles'))
pendulum.tomorrow()
# DateTime(2019, 3, 28, 0, 0, 0, tzinfo=Timezone('America/Los_Angeles'))
有各种格式选项可供选择。
pendulum.now().to_date_string()
# '2019-03-27'
pendulum.now().to_formatted_date_string()
# 'Mar 27, 2019'
pendulum.now().to_day_datetime_string()
# 'Wed, Mar 27, 2019 12:04 AM'
很多pandas用户会遇到这个问题,因为他们认为这是一个python问题,而不是pandas问题。这个答案旨在帮助那些已经使用这些库并且对知道在库本身的范围内实现这些结果感兴趣的人们。
如果你还没有使用pandas或pendulum,我绝对不建议仅仅为了运行这段代码而安装它们!这些库很重,并且在底层有很多管道。当你可以使用标准库时,这样做不值得麻烦。
now = pendulum.now()
,now = now.format('YYYY-MM-DD')
。 - Nairumfrom datetime import datetime
date = datetime.today().date()
print(date)
str(datetime.date.today())
。 - MarkHufrom datetime import date
print(f'{date.today():%Y-%m-%d}')
摘自Python f-string格式化与inline strftime不兼容,其中包含官方链接。
from datetime import datetime
import pytz
tz = pytz.timezone('US/Pacific')
datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
# '2021-09-02 10:21:41'
我的代码有点复杂,但我经常使用它。
strftime("%y_%m_%d", localtime(time.time()))
reference:'https://strftime.org/
你可以参考这个文档来制作任何你想要的东西。 对于你来说,如果想要把日期格式改成YYYY-MM-DD,只需修改我的代码为:strftime("%Y-%m-%d", localtime(time.time()))