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

如何获取日历组件标题中显示的当前月份?

获取日历组件标题显示的当前月份的解决方案
  • 直接解析视图标题
    若你的日历组件提供了获取标题文本的API(比如FullCalendar的info.view.title),可直接从标题字符串中提取月份信息。示例代码如下:

    // 以FullCalendar为例,解析标题中的月份
    const title = info.view.title;
    // 针对"2024年6月"这类格式提取月份
    const monthMatch = title.match(/(\d{1,2})月/);
    const targetMonth = monthMatch ? monthMatch[1] : null;
    // 若需年份,可同时提取
    const yearMatch = title.match(/(\d{4})年/);
    const targetYear = yearMatch ? yearMatch[1] : null;
    
  • 通过视图活跃日期范围计算
    多数日历组件会提供视图的活跃日期区间(比如info.view.activeStart和info.view.activeEnd),取区间的中间日期,其所属月份即为标题显示的月份(不受显示区域第一天跨月影响)。示例代码:

    const activeStart = info.view.activeStart;
    const activeEnd = info.view.activeEnd;
    // 计算区间中间日期
    const midTimestamp = (activeStart.getTime() + activeEnd.getTime()) / 2;
    const midDate = new Date(midTimestamp);
    // 获取实际月份(注意JS中月份从0开始,需加1)
    const currentMonth = midDate.getMonth() + 1;
    const currentYear = midDate.getFullYear();
    
  • 检查组件内置属性
    部分日历组件会直接暴露当前视图的月份属性(比如info.view.currentMonth),优先查看组件文档确认是否有此类直接可用的属性,这是最简便的方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 12:42:39