【问题标题】:python Datetime - extract year and monthpython Datetime - 提取年份和月份
【发布时间】:2019-11-29 09:15:26
【问题描述】:

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

sales_close_7 = 2018-12-07

【问题讨论】:

  • 我需要日期时间格式而不是字符串

标签: python-3.x python-datetime


【解决方案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

【讨论】:

  • 兄弟我需要日期时间格式的“2018-12”格式,你能帮帮我吗?
  • @affadamseven new_date = str(date_object.year)+'-'+str(date_object.month)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多