【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
这是我的代码:
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
这样的简单约会?
#1楼
import datetime
print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
编辑:
在Cees建议之后,我也开始使用时间:
import time
print time.strftime("%Y-%m-%d %H:%M")
#2楼
date,datetime和time对象均支持strftime(format)方法,以在显式格式字符串的控制下创建表示时间的字符串。
这是格式代码及其指令和含义的列表。
%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
#3楼
这更短:
>>> import time
>>> time.strftime("%Y-%m-%d %H:%M")
'2013-11-19 09:38'
#4楼
甚至
from datetime import datetime, date
"{:%d.%m.%Y}".format(datetime.now())
出:'25 .12.2013
要么
"{} - {:%d.%m.%Y}".format("Today", datetime.now())
离开:“今天-2013年12月25日”
"{:%A}".format(date.today())
出:“星期三”
'{}__{:%Y.%m.%d__%H-%M}.log'.format(__name__, datetime.now())
出:'__main ____ 2014.06.09__16-56.log'
#5楼
由于print today
的print today
返回了您想要的内容,因此这意味着今天的对象的__str__
函数返回了您要查找的字符串。
因此,您也可以执行mylist.append(today.__str__())
。
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3150928