You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于起始日期与星期起始日的财年周数计算需求

Calculating Fiscal Year Week Number & Rest Day Status

Got it, let's break down how to solve this problem step by step. The core requirements are:

  • Determine which fiscal week a given date falls into, based on a custom fiscal year start date and week start day (Monday or Sunday)
  • Check if the date is a rest day (usually weekends, but we'll make this configurable)

First, since the full week number configuration table was cut off, I'll use common fiscal week logic as a baseline:

  • A fiscal week is 7 days long, starting on either Monday or Sunday
  • The first fiscal week of the year is the week that contains the fiscal start date
  • Week numbers increment sequentially from 1 to 52/53 depending on the year

Step 1: Core Helper Functions

I'll use Python (it's ideal for date manipulation) to implement this, leveraging the built-in datetime module.

from datetime import datetime, timedelta

def get_week_start(date: datetime, week_start_is_monday: bool) -> datetime:
    """Adjust a date to the start of its week (Monday or Sunday)"""
    if week_start_is_monday:
        # ISO week starts on Monday; subtract days based on weekday (0=Monday)
        days_to_subtract = date.weekday()
    else:
        # Sunday as start: adjust using weekday() (6=Sunday)
        days_to_subtract = (date.weekday() + 1) % 7
    return date - timedelta(days=days_to_subtract)

def calculate_fiscal_week(target_date: datetime, fiscal_start_date: datetime, week_start_is_monday: bool) -> int:
    """Calculate the fiscal week number for the target date"""
    # Get week start dates for both target and fiscal start
    target_week_start = get_week_start(target_date, week_start_is_monday)
    fiscal_start_week_start = get_week_start(fiscal_start_date, week_start_is_monday)
    
    # Calculate weeks between the two week starts
    delta_weeks = (target_week_start - fiscal_start_week_start).days // 7
    
    # Fiscal week number = delta + 1 (since fiscal start week is week 1)
    fiscal_week = delta_weeks + 1
    
    # Handle dates before the fiscal start (extend logic for prior fiscal years if needed)
    if fiscal_week < 1:
        raise ValueError("Target date is before the fiscal start date")
    
    return fiscal_week

def is_rest_day(date: datetime, rest_days: list[int] = [5,6]) -> bool:
    """Check if a date is a rest day. Defaults to Saturday (5) and Sunday (6)"""
    return date.weekday() in rest_days

Step 2: Example Usage

Let's test this with sample inputs to verify:

# Example 1: Fiscal year starts 2024-04-01, week starts on Monday
fiscal_start = datetime(2024, 4, 1)
target_date = datetime(2024, 4, 5)  # Friday

print(f"Fiscal week number: {calculate_fiscal_week(target_date, fiscal_start, week_start_is_monday=True)}")  # Output: 1
print(f"Is rest day? {is_rest_day(target_date)}")  # Output: False

# Example 2: Week starts on Sunday, target is 2024-04-07 (Sunday)
target_date_sun = datetime(2024, 4, 7)

print(f"Fiscal week number: {calculate_fiscal_week(target_date_sun, fiscal_start, week_start_is_monday=False)}")  # Output: 2
print(f"Is rest day? {is_rest_day(target_date_sun)}")  # Output: True

Key Notes

  • Prior Fiscal Year Handling: If you need to support dates before the fiscal start date, extend the calculate_fiscal_week function to roll back to the previous fiscal year and compute the week count relative to that.
  • Custom Rest Days: The is_rest_day function accepts a list of weekday integers (0=Monday, 6=Sunday) so you can configure non-standard rest days (e.g., [4] for Friday if required).
  • Edge Cases: Leap years and 53-week years are handled automatically since the logic uses actual day differences between week starts.

If your configuration table has specific rules (like fiscal weeks aligning to fixed monthly days instead of the week containing the start date), let me know and we can tweak the logic to match!

内容的提问来源于stack exchange,提问作者Niteesh Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:58:22