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

财务年月选择:往期月份天数、已过天数展示及剩余天数置零需求

实现财年非当前月份的天数统计逻辑

核心逻辑

  1. 先明确财年起始月份(比如部分企业财年从4月开始,需提前配置),判断选择的月份是否属于当前财年的当前自然月
  2. 对非当前月份:
    • 总天数:目标月份的实际天数(通过日期工具类计算,无需历史数据)
    • 已过天数:直接等于总天数(往期月份已完全度过)
    • 剩余天数:固定为0
  3. 对当前月份:用当前日期计算已过天数(当前日),剩余天数=总天数-已过天数

JavaScript 实现示例

// 配置财年起始月份(自然年则设为1)
const FY_START_MONTH = 1;

// 计算指定年月的总天数
function getTotalDays(year, month) {
    // 利用Date对象特性:下一个月的第0天就是当前月的最后一天
    return new Date(year, month, 0).getDate();
}

// 判断选择的月份是否为当前财年的当前月份
function isCurrentFYMonth(selectedYear, selectedMonth) {
    const now = new Date();
    const currentYear = now.getFullYear();
    const currentMonth = now.getMonth() + 1; // 转为1-12的月份格式

    // 处理财年跨年度情况(比如财年4月到次年3月)
    let currentFYYear = currentYear;
    if (currentMonth < FY_START_MONTH) {
        currentFYYear = currentYear - 1;
    }

    return selectedYear === currentFYYear && selectedMonth === currentMonth;
}

// 获取最终天数统计数据
function getDaysStats(selectedYear, selectedMonth) {
    const totalDays = getTotalDays(selectedYear, selectedMonth);
    if (!isCurrentFYMonth(selectedYear, selectedMonth)) {
        return {
            totalDays,
            passedDays: totalDays,
            remainingDays: 0
        };
    } else {
        const currentDay = new Date().getDate();
        return {
            totalDays,
            passedDays: currentDay,
            remainingDays: totalDays - currentDay
        };
    }
}

// 示例:调用函数获取2024年3月的统计数据(非当前月)
console.log(getDaysStats(2024, 3));
// 输出:{ totalDays: 31, passedDays: 31, remainingDays: 0 }

Python 实现示例

from datetime import datetime, timedelta

# 配置财年起始月份(自然年则设为1)
FY_START_MONTH = 1

def get_total_days(year, month):
    # 通过计算下一个月第一天的前一天,得到当月最后一天的天数
    if month == 12:
        next_month = datetime(year + 1, 1, 1)
    else:
        next_month = datetime(year, month + 1, 1)
    last_day = next_month - timedelta(days=1)
    return last_day.day

def is_current_fy_month(selected_year, selected_month):
    now = datetime.now()
    current_year = now.year
    current_month = now.month

    # 处理财年跨年度情况
    current_fy_year = current_year
    if current_month < FY_START_MONTH:
        current_fy_year = current_year - 1

    return selected_year == current_fy_year and selected_month == current_month

def get_days_stats(selected_year, selected_month):
    total_days = get_total_days(selected_year, selected_month)
    if not is_current_fy_month(selected_year, selected_month):
        return {
            "totalDays": total_days,
            "passedDays": total_days,
            "remainingDays": 0
        }
    else:
        current_day = datetime.now().day
        return {
            "totalDays": total_days,
            "passedDays": current_day,
            "remainingDays": total_days - current_day
        }

# 示例:调用函数获取2024年3月的统计数据(非当前月)
print(get_days_stats(2024, 3))
# 输出:{'totalDays': 31, 'passedDays': 31, 'remainingDays': 0}

关键说明

  • 无需历史数据:任意月份的总天数都可以通过日期工具类动态计算,不用提前存储
  • 财年适配:只需修改FY_START_MONTH参数,即可适配不同起始月份的财年
  • 边界处理:自动兼容闰年、不同月份天数差异的情况

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 09:05:25