python Datetime-提取年份和月份

时间:2019-11-29 09:15:26

标签: python-3.x python-datetime

如何从string邮票格式的DateTime中提取月份和年份?

sales_close_7 = 2018-12-07

1 个答案:

答案 0 :(得分:1)

import datetime

sales_close_7 = '2018-12-07'
date_object = datetime.datetime.strptime(sales_close_7, '%Y-%m-%d').date()

print(date_object.year)
Output: 2018
print(date_object.month)
Output: 12