Antd Calendar 获取面板上的第一个日期和最后一个日期

文章讲述了如何在使用AntDesign(antd)的Calendar组件时,获取指定面板上的起始日期(如2023-11-27)和结束日期(如2024-01-07),通过moment.js处理日期逻辑实现。
摘要由CSDN通过智能技术生成

最近写需求的时候遇到了使用antd Calendar 组件需要获取到面板上第一个日期以及最后一个日期

例如:下图需要获取 2023-11-27到2024-1-7

具体代码:

import moment, { Moment } from 'moment';
import { Calendar } from 'antd';
const MobilePayment = (props: any) => {
  const handlePanelChange = (date: Moment, mode: CalendarMode) => {
    const { firstDate, lastDate } = getFirstDateAndLastDateOnThePanel(date);
    console.log("hihiihi", {
      firstDate,
      lastDate
    })
  }
  // 获取面板的起始日期和结束日期
  const getFirstDateAndLastDateOnThePanel = (date: Moment) => {
    // 这个月的第一天
    const firstDateOfMonth = moment(date).startOf("month")
    // 第一天是星期几
    const firstdayOfWeek = Number(firstDateOfMonth.day()) === 0 ? 7 : Number(firstDateOfMonth.day())
    // console.log('firstdayOfWeek',firstdayOfWeek)
    let dateInMonth: Moment = firstDateOfMonth;
    // 如果是星期一的话 就是这个月的第一天
    if (firstdayOfWeek !== 1) {
      dateInMonth = date.month(date.month() - 1).date(moment(date).subtract(1, 'month').daysInMonth() - firstdayOfWeek + 2);
    }

    // 计算有多少天是下个月的
    const restPassedDay = 42 - firstdayOfWeek + 1 - Number(moment(date).daysInMonth());
    // 计算下个月的日期
    const lastDate = date.month(date.month() + 1).date(restPassedDay);

    return {
      firstDate: dateInMonth.format('YYYY-MM-DD'),
      lastDate: lastDate.format('YYYY-MM-DD'),
    };
  };

  return (
    <>
      <Calendar
        mode="month"
        onPanelChange={handlePanelChange}
      />
    </>
  )
}

具体结果打印:

好的,我可以为您提供一个简单的React组件,该组件使用Antd的Collapse面板实现折叠面板,并允许用户添加或删除面板。 首先,我们需要安装ReactAntd库: ``` npm install react antd ``` 然后,我们可以创建一个叫做"CollapsiblePanel"的组件: ``` import React, { useState } from "react"; import { Collapse, Button } from "antd"; const { Panel } = Collapse; const CollapsiblePanel = () => { const [panels, setPanels] = useState([]); const handleAddPanel = () => { setPanels([...panels, `Panel ${panels.length + 1}`]); }; const handleRemovePanel = (index) => { const newPanels = [...panels]; newPanels.splice(index, 1); setPanels(newPanels); }; return ( <div> <Button onClick={handleAddPanel}>Add Panel</Button> <Collapse> {panels.map((panel, index) => ( <Panel header={panel} key={index}> <Button onClick={() => handleRemovePanel(index)}>Remove</Button> </Panel> ))} </Collapse> </div> ); }; export default CollapsiblePanel; ``` 在这个组件中,我们使用useState钩子来追踪当前面板的列表。当用户点击"Add Panel"按钮时,我们使用setPanels更新面板列表。每个面板都可以包含一个"Remove"按钮,用来删除该面板。我们通过使用map函数,将每个面板的信息渲染为一个Antd Collapse.Panel组件。 最后,我们可以在我们的应用程序中使用CollapsiblePanel组件: ``` import React from "react"; import ReactDOM from "react-dom"; import CollapsiblePanel from "./CollapsiblePanel"; ReactDOM.render(<CollapsiblePanel />, document.getElementById("root")); ``` 这将在您的应用程序中呈现一个包含"Add Panel"按钮可折叠面板的组件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值