如何根据日期字符串输入获取月份中的天数

时间:2018-10-10 15:42:00

标签: python python-3.x pandas

我正在尝试使代码自动化。我希望代码从月初开始到前一天结束为止提取数据。目前,我正在使用以下命令获取结束日期:

<p>what&shy;ever&shy;wonder&shy;ful&shy;long&shy;word what&shy;ever&shy;wonder&shy;ful&shy;long&shy;word what&shy;ever&shy;wonder&shy;ful&shy;long&shy;word what&shy;ever&shy;wonder&shy;ful&shy;long&shy;word what&shy;ever&shy;wonder&shy;ful&shy;long&shy;word what&shy;ever&shy;wonder&shy;ful&shy;long&shy;word what&shy;ever&shy;wonder&shy;ful&shy;long&shy;word what&shy;ever&shy;wonder&shy;ful&shy;long&shy;word</p>

如何根据今天回到月初的情况来区分代码?而且,如何告诉代码是否是该月的第一天返回前几个月的数据?

要获得奖金,一旦有了开始日期和结束日期,返回怎么找到一个月中的天数?我已经尝试过此命令,但它不想在单个日期上工作。

dateEnd = pd.to_datetime('today')

1 个答案:

答案 0 :(得分:0)

这将为您提供所需日期:

import datetime

end = datetime.date.today() - datetime.timedelta(1)
start = end.replace(day=1)
daysInMonth = (datetime.date(start.year, start.month + 1, 1) - datetime.timedelta(1)).day

start
#2018-10-01

end
#2018-10-09

daysInMonth
#31