>> from datetime import datetime
>> now = datetime.now()
>> now.strftime('%Y-%m-%d')
'2018-01-29'
如果此时我们想转换为 2018-1-29
的形式,有如下的两种方式:
法 1:分别转换为 int 型以消除前导 0:
year = str(int(now.strftime('%Y'))) month = str(int(now.strftime('%m'))) day = str(int(now.strftime('%d'))) year+'-'+month+'-'+day
法二:使用 datetime 下的结构体:
>> now = now.timetuple() >> now time.struct_time(tm_year=2018, tm_mon=1, tm_mday=29, tm_hour=0, tm_min=24, tm_sec=0, tm_wday=0, tm_yday=29, tm_isdst=-1) >> year = str(now.tm_year) >> month = str(now.tm_mon) >> day = str(now.tm_mday)