how to get first day and last day of month on given user input date in python?

2606 views python
2

user input for start time and end time. Dates input format ""%Y-%m-%dT%H:%M:%S"

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

after conversion it should look like:-

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

answered question

Any one can suggest.

1 Answer

13

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)

posted this

Have an answer?

JD

Please login first before posting an answer.