Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to achieve quarterly start and end time by python

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report