How to achieve quarterly start and end time by python
This article introduces the relevant knowledge of "how to achieve quarterly statistics by python". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Actual combat scene
Require quarterly statistics on the consumption of agents from the date of registration.
Abstract scene
The start and end time of each quarter from the default time point
Investigation point
The use of datetime Library
Yield
Code implementation
From datetime import datetime
Import calendar
Def startDate (cls, dazzles 2017-03-01 0015):
Init = datetime.strptime (str,'% Y-%m-%d% HGV% MVA% S') # default time
Now = datetime.now () # current time
Start = now.replace (day=1) # the beginning of the current month
Days = calendar.monthrange (start.year, start.month) # days of the month
End = start + timedelta (days [1]) # end of the current month
While end > init:
Result = (start, end)
Yield result
End = start
Start = (start-timedelta (1)) .replace (day=1)
Implementation description
Flexible application of time object
You can also use a list implementation, but I used the yield implementation to deepen my understanding of yield
This is the end of the content of "how to achieve the quarterly start and end time of python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!