Skip to content
Discussion options

You must be logged in to vote

尝试实现了这个需求,感觉代码量还好 (?)

import React from 'react';
import dayjs from 'dayjs';
import { DatePicker } from 'tdesign-react';

export default function DatePickerDemo() {
  const disableTime = (date: Date) => {
    const now = dayjs();
    const selected = dayjs(date);

    // 只有选中的是今天,才需要禁用时间
    if (!selected.isSame(now, 'day')) return {};

    const currentHour = now.hour();
    const currentMinute = now.minute();
    const currentSecond = now.second();

    const selectedHour = selected.hour();
    const selectedMinute = selected.minute();

    return {
      // 禁用当前小时之前的所有小时
      hour: Array.from({ length: currentHour }, (_, i) => i),
      // 当面板当前滚动到的小时 === 当前小时时,禁用当前分钟之前的所有分钟
      minute

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by kajweb
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants