Python獲取年月日用到datetime模塊
獲取年月日
current_date = datetime.datetime.now().strftime('%Y-%m-%d')
獲取年月
current_date = datetime.datetime.now().strftime('%Y-%m')
current_date = datetime.datetime.now().date()
獲取年份
current_date = datetime.datetime.now().strftime('%Y')
第二種
d1 = datetime.date.today()
print(f'日期所屬的年份為 : {d1.year}')
print(f'日期所屬的月份為 : {d1.month}')
print(f'日期具體的日期號為 : {d1.day}')
