如何在 python 中从下一个季度开始自动创建季度(Q022022)列表

发布于 2025-01-10 00:09:09 字数 138 浏览 0 评论 0原文

抱歉,我不是 python 专家。我想自动创建以下列表,并且第一季度必须始终是下一个季度。因此,如果我今天(23/02/22)执行此操作,第一个将是 Q022022,如果我在日期 23/05/22 执行此操作,第一个将是 Q032022。

先感谢您

I am sorry but I am not an expert of python. I would like to create the following list in automatic and the first quarter need to be always the next one. So if I do it today (23/02/22) the first one will be Q022022 if I do it in date 23/05/22 the first one need to be Q032022.

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2025-01-17 00:09:09

获取季度

current_quarter = ((month-1) // 3) + 1
next_quarter = current_quarter + 1

如果是上个季度 (4),则需要使用 modulo (%) 来获取第一季度 (1),并且需要增加 year

next_quarter = ((next_quarter-1) % 4) + 1

if next_quarter < current_quarter:
   year = year + 1

您可以直接从字符串 ` 获取 monthyear

    date = "23/02/22"

    month = int(date[3:5])
    year  = int(date[7:9]) + 2000

或者您可以使用 datetime 解析它

    date = "23/02/22"

    dt = datetime.datetime.strptime(date, '%d/%m/%y')

    month = dt.month
    year  = dt.year

最小工作示例

import datetime

test = [
    ('23/01/22', 'Q022022'),
    ('23/02/22', 'Q022022'),
    ('23/03/22', 'Q022022'),
    
    ('23/04/22', 'Q032022'),
    ('23/05/22', 'Q032022'),
    ('23/09/22', 'Q042022'),
    ('23/12/22', 'Q012023'),
]

for date, expect in test:
    print('date:', date, '| expect:', expect)
    
    dt = datetime.datetime.strptime(date, '%d/%m/%y')
    m = dt.month
    y = dt.year

    #m = int(date[3:5])
    #y = int(date[7:9]) + 2000
    
    #print(dt, m, y)

    current_q = ((m-1) // 3) + 1    # use months 0..11
    next_q = current_q + 1
    next_q = ((next_q-1) % 4) + 1   # use quarters 0..3
    #next_q = (current_q % 4) + 1
    
    print('current:', current_q, '| next:', next_q)
    
    if next_q < current_q:
        y += 1
        
    result = f'Q{next_q:02}{y}'
    
    print('result:', result, '| expect:', expect, '| comparition:', result == expect)
    print('---')

结果:

date: 23/01/22 | expect: Q022022
current: 1 | next: 2
result: Q022022 | expect: Q022022 | comparition: True
---
date: 23/02/22 | expect: Q022022
current: 1 | next: 2
result: Q022022 | expect: Q022022 | comparition: True
---
date: 23/03/22 | expect: Q022022
current: 1 | next: 2
result: Q022022 | expect: Q022022 | comparition: True
---
date: 23/04/22 | expect: Q032022
current: 2 | next: 3
result: Q032022 | expect: Q032022 | comparition: True
---
date: 23/05/22 | expect: Q032022
current: 2 | next: 3
result: Q032022 | expect: Q032022 | comparition: True
---
date: 23/09/22 | expect: Q042022
current: 3 | next: 4
result: Q042022 | expect: Q042022 | comparition: True
---
date: 23/12/22 | expect: Q012023
current: 4 | next: 1
result: Q012023 | expect: Q012023 | comparition: True
---

To get quarter

current_quarter = ((month-1) // 3) + 1
next_quarter = current_quarter + 1

If it was last quarter (4) then it needs to use modulo (%) to get first quarter (1), and it needs to increase year

next_quarter = ((next_quarter-1) % 4) + 1

if next_quarter < current_quarter:
   year = year + 1

You could get month and year directly from string `

    date = "23/02/22"

    month = int(date[3:5])
    year  = int(date[7:9]) + 2000

or you can use datetime to parse it

    date = "23/02/22"

    dt = datetime.datetime.strptime(date, '%d/%m/%y')

    month = dt.month
    year  = dt.year

Minimal working example

import datetime

test = [
    ('23/01/22', 'Q022022'),
    ('23/02/22', 'Q022022'),
    ('23/03/22', 'Q022022'),
    
    ('23/04/22', 'Q032022'),
    ('23/05/22', 'Q032022'),
    ('23/09/22', 'Q042022'),
    ('23/12/22', 'Q012023'),
]

for date, expect in test:
    print('date:', date, '| expect:', expect)
    
    dt = datetime.datetime.strptime(date, '%d/%m/%y')
    m = dt.month
    y = dt.year

    #m = int(date[3:5])
    #y = int(date[7:9]) + 2000
    
    #print(dt, m, y)

    current_q = ((m-1) // 3) + 1    # use months 0..11
    next_q = current_q + 1
    next_q = ((next_q-1) % 4) + 1   # use quarters 0..3
    #next_q = (current_q % 4) + 1
    
    print('current:', current_q, '| next:', next_q)
    
    if next_q < current_q:
        y += 1
        
    result = f'Q{next_q:02}{y}'
    
    print('result:', result, '| expect:', expect, '| comparition:', result == expect)
    print('---')

Result:

date: 23/01/22 | expect: Q022022
current: 1 | next: 2
result: Q022022 | expect: Q022022 | comparition: True
---
date: 23/02/22 | expect: Q022022
current: 1 | next: 2
result: Q022022 | expect: Q022022 | comparition: True
---
date: 23/03/22 | expect: Q022022
current: 1 | next: 2
result: Q022022 | expect: Q022022 | comparition: True
---
date: 23/04/22 | expect: Q032022
current: 2 | next: 3
result: Q032022 | expect: Q032022 | comparition: True
---
date: 23/05/22 | expect: Q032022
current: 2 | next: 3
result: Q032022 | expect: Q032022 | comparition: True
---
date: 23/09/22 | expect: Q042022
current: 3 | next: 4
result: Q042022 | expect: Q042022 | comparition: True
---
date: 23/12/22 | expect: Q012023
current: 4 | next: 1
result: Q012023 | expect: Q012023 | comparition: True
---
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文