Python获取年月日

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}')

  

  

原文地址:https://www.cnblogs.com/lucktomato/p/15405610.html