这是我的代码:

import datetime
today = datetime.date.today()
print(today)

这张照片是:2008-11-22,这正是我想要的。

但是,我有一个列表,我要将其附加到列表中,然后突然一切都变得“不稳定”。代码如下:

import datetime
mylist = []
today = datetime.date.today()
mylist.append(today)
print(mylist)

这将打印以下内容:

[datetime.date(2008, 11, 22)]

我怎样才能得到像2008-11-22这样的简单约会?


当前回答

考虑到你要求一些简单的事情来做你想做的事情,你可以:

import datetime
str(datetime.date.today())

其他回答

在格式化字符串文本中使用特定于类型的日期时间字符串格式(参见nk9使用str.format()的答案)(自Python 3.6,2016-12-23以来):

>>> import datetime
>>> f"{datetime.datetime.now():%Y-%m-%d}"
'2017-06-15'

日期/时间格式指令没有作为格式字符串语法的一部分记录,而是记录在日期、日期时间和时间的strftime()文档中。基于1989年C标准,但包括自Python 3.6以来的一些ISO 8601指令。

date、datetime和time对象都支持strftime(格式)方法,在显式格式的控制下创建表示时间的字符串一串

以下是格式代码及其指令和含义的列表。

%a  Locale’s abbreviated weekday name.
%A  Locale’s full weekday name.      
%b  Locale’s abbreviated month name.     
%B  Locale’s full month name.
%c  Locale’s appropriate date and time representation.   
%d  Day of the month as a decimal number [01,31].    
%f  Microsecond as a decimal number [0,999999], zero-padded on the left
%H  Hour (24-hour clock) as a decimal number [00,23].    
%I  Hour (12-hour clock) as a decimal number [01,12].    
%j  Day of the year as a decimal number [001,366].   
%m  Month as a decimal number [01,12].   
%M  Minute as a decimal number [00,59].      
%p  Locale’s equivalent of either AM or PM.
%S  Second as a decimal number [00,61].
%U  Week number of the year (Sunday as the first day of the week)
%w  Weekday as a decimal number [0(Sunday),6].   
%W  Week number of the year (Monday as the first day of the week)
%x  Locale’s appropriate date representation.    
%X  Locale’s appropriate time representation.    
%y  Year without century as a decimal number [00,99].    
%Y  Year with century as a decimal number.   
%z  UTC offset in the form +HHMM or -HHMM.
%Z  Time zone name (empty string if the object is naive).    
%%  A literal '%' character.

这是我们可以使用Python中的datetime和time模块所做的

import time
import datetime

print "Time in seconds since the epoch: %s" %time.time()
print "Current date and time: ", datetime.datetime.now()
print "Or like this: ", datetime.datetime.now().strftime("%y-%m-%d-%H-%M")

print "Current year: ", datetime.date.today().strftime("%Y")
print "Month of year: ", datetime.date.today().strftime("%B")
print "Week number of the year: ", datetime.date.today().strftime("%W")
print "Weekday of the week: ", datetime.date.today().strftime("%w")
print "Day of year: ", datetime.date.today().strftime("%j")
print "Day of the month : ", datetime.date.today().strftime("%d")
print "Day of week: ", datetime.date.today().strftime("%A")

这将打印出如下内容:

Time in seconds since the epoch:    1349271346.46
Current date and time:              2012-10-03 15:35:46.461491
Or like this:                       12-10-03-15-35
Current year:                       2012
Month of year:                      October
Week number of the year:            40
Weekday of the week:                3
Day of year:                        277
Day of the month :                  03
Day of week:                        Wednesday

对于那些希望基于区域设置的日期而不包括时间的用户,请使用:

>>> some_date.strftime('%x')
07/11/2019

以下是如何将日期显示为(年/月/日):

from datetime import datetime
now = datetime.now()

print '%s/%s/%s' % (now.year, now.month, now.day)

也许最短的解决方案是:

mylist.append(str(AnyDate)[:10])

或甚至更短,例如:

f'{AnyDate}'[:10]

PS:不需要今天。