首页 > 解决方案 > 在给定的用户输入日期获取一个月的第一天和最后一天

问题描述

开始时间和结束时间的用户输入。

日期输入格式,"%Y-%m-%dT%H:%M:%S

st = 2018-10-10T00:00:00
et = 2018-03-30T23:59:59

转换后它应该看起来像:-

st1 = 2018-10-01T00:00:00 --> first day of month
et1 =  2018-06-31T23:59:59 ---> last day of month

标签: pythonpython-3.xdatetime

解决方案


I find arrow module most efficient for this kind of purpose.

import arrow

st = "2018-10-10T00:00:00"
et = "2018-03-30T23:59:59"


first_day = arrow.get(st).floor('month')
last_day = arrow.get(et).ceil('month')

print(first_day)
print(last_day)

# output

2018-10-01T00:00:00+00:00
2018-03-31T23:59:59.999999+00:00

推荐阅读