gpt4 book ai didi

python - 将日期转换为月份和年份

转载 作者:太空宇宙 更新时间:2023-11-03 15:26:11 28 4
gpt4 key购买 nike

从以下日期如何使用python获取月份和年份

日期是

           "2011-07-01 09:26:11"   //This showud display  as "This month"
"2011-06-07 09:26:11" //This should display as June
"2011-03-12 09:26:11" //This should display as March
"2010-07-25 09:26:11" // Should display as last year

请告诉我如何使用 python 2.4 获取这些格式

当我在 pandas 数据框中有一个日期变量[格式相同]时,如何执行此操作?

最佳答案

import time
import calendar

current = time.localtime()

dates = ["2011-07-01 09:26:11", "2011-06-07 09:26:11", "2011-03-12 09:26:11", "2010-07-25 09:26:11"]

for date in dates:
print "%s" % date
date = time.strptime(date, "%Y-%m-%d %H:%M:%S")

if date.tm_year == current.tm_year - 1:
print "Last year"

elif date.tm_mon == current.tm_mon:
print "This month"

else:
print calendar.month_name[date.tm_mon]

应该大致按照您的要求去做。

关于python - 将日期转换为月份和年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6556581/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com